SQL Query Shortcuts

What are some great SQL acronyms that you know about?

For example, for something that I learned today, you can specify a group by index:

SELECT col1, col2 FROM table GROUP BY 2

This will be the col2 group

+1
source share
3 answers

See Aaron Bertrand's article β€œBad SQL Habits for a Hit” - he has a message about this and strongly recommends not using this notation:

http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/06/bad-habits-to-kick-order-by-ordinal.aspx

Mark

+3
source

- , SELECT, GROUP BY ORDER BY. , , , .

. IE:

SELECT
  FROM THIS_IS_MY_UNGODLY_TABLE_NAME timutn

... , timutn , , / , . , , ?

, , IDE- SQL intellisense , .

+2

, WHERE 1=1

,

$query = 'SELECT * FROM table ';

<?php if ( $something ) { ?>
$sql.= 'WHERE something = ' . $variable;
<?php } else { ?>
$sql.= 'WHERE something = ' . $another;
<?php } ?>

If there are many branches there, it can be such a pain (and I do not believe in building query strings, but this is just an example).

A simple way:

$query = 'SELECT * FROM table WHERE 1=1 ';

<?php if ( $something ) { ?>
$sql.= 'AND something = ' . $variable;
<?php } else { ?>
$sql.= 'AND something = ' . $another;
<?php } ?>

Of course, I would probably use a wrapper / class that has a method wherein which I can specify conditions.

+2
source

All Articles