温故知新-MYSQL-一些查询技巧

2022-07-28  本文已影响0人  gao922699

查询表里的重复值:

SELECT 
    first_name, COUNT(first_name),
    last_name,  COUNT(last_name),
    email,      COUNT(email)
    FROM contacts 
    GROUP BY first_name , last_name , email
    HAVING  COUNT(first_name) > 1
    AND COUNT(last_name) > 1
    AND COUNT(email) > 1;

查询两个表里的差异值:

SELECT id,title FROM (
    SELECT id, title FROM t1
    UNION ALL
    SELECT id,title FROM t2) AS tbl
    GROUP BY id, title 
    HAVING count(*) = 1
    ORDER BY id;

UUID()和自增INT主键的对比

优点

使用UUID作为主键具有以下优点:

缺点

除了优势之外,UUID值也有一些缺点:

复制表数据

老表数据复制到新表

CREATE TABLE new_table 
SELECT col1, col2, col3 
FROM
    existing_tableWHERE
    conditions;

正则查询

REGEXP/NOT REGEXP

SELECT 
    column_list
    FROM
    table_name
    WHERE
    string_column REGEXP pattern;

获取第N个最高纪录

SELECT * FROM table_name ORDER BY column_name DESC LIMIT n - 1, 1;

NULL值替换

SELECT customername, 
       IFNULL(state,"N/A") state, 
       countryFROM customersORDER BY country;
上一篇 下一篇

猜你喜欢

热点阅读