常用SQL语句整理.md
    查询 A 表和 B 表中有,但是不在 C 表中的,A,B,C 都有字段 d

    1. select * from A.a
    2. left join B.b
    3. on a.d = b.d
    4. where d not in (
    5. select a.d from A a,C c where a.d = c.d
    6. )

    1. select * from A.a
    2. left join B.b
    3. on a.c = b.c and b.d > 20
    4. where

    主键自增,查询最后一条记录

    1. select top 1 * from user order by id desc;
    2. select * from user order by limit 1;

    查询一天的数据:

    1. select * from tableA where to_days(column_time) = to_days(now());
    2. select * from tableA where date(cloumn_time) = curdate();

    查询出近一周的数据

    1. select * from tableA where TO_DAYS(now()) - TO_DAYS(datecolumn) <=7;
    2. select * from tableA where DATA_SUB(curdate(),INTERVAL 7 DAY) <= date(datecolumn)

    //sqlserver

    1. select * from where datediff(week,时间字段,getDate()) = 0;

    查询出近一个月的数据

    1. select * from tableA where data_sub(curdate(),INTERVAL 30 DAT) <= date(datecolumn)

    查询出近一年的数据:

    1. select * from tableA where DATE_SUB(curdate(),INTRRVAL 1 YEAR) <= date(datecolumn)
    2. select * from tableA where YEAR(datecolumn) = YEAR(NOW());