Group by date only on a Datetime column

Cast the datetime to a date, then GROUP BY using this syntax: SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate); Or you can GROUP BY the alias as @orlandu63 suggested: SELECT SUM(foo), DATE(mydate) DateOnly FROM a_table GROUP BY DateOnly; Though I don’t think it’ll make any difference to performance, it is a little clearer.

Use ‘=’ or LIKE to compare strings in SQL?

LIKE and the equality operator have different purposes, they don’t do the same thing: = is much faster, whereas LIKE can interpret wildcards. Use = wherever you can and LIKE wherever you must. SELECT * FROM user WHERE login LIKE ‘Test%’; Sample matches: TestUser1 TestUser2 TestU Test

In SQL, what’s the difference between count(column) and count(*)?

count(*) counts NULLs and count(column) does not [edit] added this code so that people can run it create table #bla(id int,id2 int) insert #bla values(null,null) insert #bla values(1,null) insert #bla values(null,1) insert #bla values(1,null) insert #bla values(null,1) insert #bla values(1,null) insert #bla values(null,null) select count(*),count(id),count(id2) from #bla results 7 3 2

PostgreSQL wildcard LIKE for any of a list of words

PostgreSQL also supports full POSIX regular expressions: select * from table where value ~* ‘foo|bar|baz’; The ~* is for a case insensitive match, ~ is case sensitive. Another option is to use ANY: select * from table where value like any (array[‘%foo%’, ‘%bar%’, ‘%baz%’]); select * from table where value ilike any (array[‘%foo%’, ‘%bar%’, ‘%baz%’]); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)