SQL小技巧

随机选择数据

SELECT * FROM table_name order by rand() limit 20;

INNER JOIN

两个表进行inner join时,对于相同的字段,不论是用左表还是右表,相交的结果都是一样的。

SELECT  a.column_name
FROM    (
        SELECT  column_name
        FROM    table_a
        ) a
JOIN (
        SELECT  column_name
        FROM    table_b
     ) b
ON      a.column_name = b.column_name
;
SELECT  b.column_name
FROM    (
        SELECT  column_name
        FROM    table_a
        ) a
JOIN (
        SELECT  column_name
        FROM    table_b
     ) b
ON      a.column_name = b.column_name
;

上面两个SQL的运行结果是相同的。

你可能感兴趣的:(开发,sql,数据库,数据)