mysql基础知识补充

关于distinct

         是特殊的group by,在8.0之前group by会对结果集排序,distinct不会

关于like

        % 匹配任何零或多个字符的字符串,_匹配任何单个字符,转义字符:\

//find products whose product codes contain the string _20
SELECT 
    productCode, 
    productName
FROM
    products
WHERE
    productCode LIKE '%\_20%';
//或者使用 ESCAPE 
WHERE
    productCode LIKE '%$_20%' ESCAPE '$';

 关于limit与order:

关于别名alias

     看到上面这长截图大家应该知道,where不能使用别名,因为现在select中的别名还没有显圣

关于join

     方便问一下:mysql 什么时候开始支持 full  outer  join

SELECT column_list
FROM table_1
INNER JOIN table_2 ON join_condition;
等价于:
SELECT column_list
FROM table_1
INNER JOIN table_2 USING (column_name);

   cross join:关键词 笛卡尔积

SELECT 
    m.member_id, 
    c.committee_id
FROM
    members m
CROSS JOIN committees c;

self join:分层

    一次查询中引用没有别名的表两次或更多次 会报错

SELECT 
    c1.city, 
    c1.customerName, 
    c2.customerName
FROM
    customers c1
INNER JOIN customers c2 ON 
    c1.city = c2.city
    AND c1.customername > c2.customerName
ORDER BY 
    c1.city;

关于group

SELECT 
    c1, c2,..., cn, aggregate_function(ci)
FROM
    table
WHERE
    where_conditions
GROUP BY c1 , c2,...,cn;

SELECT 
    YEAR(orderDate) AS year,
    SUM(quantityOrdered * priceEach) AS total
FROM
    orders
INNER JOIN orderdetails 
    USING (orderNumber)
WHERE
    status = 'Shipped'
GROUP BY 
    year
HAVING 
    year > 2003;

rollup:类似union吧(当然是不太对的说法)

     于order by互斥(待考证https://www.mysqltutorial.org/mysql-rollup/

     为分组以后的小组进行数据统计,相对于简单的分组合计增加了小计和合计,当针对一个字段分组的时候则统计一次,如果是针对多个字段进行的分组那么最后需要针对每一个分组进行一次统计

grouping(field)

    当字段为null返回1,否则返回0

SELECT 
    orderYear,
    productLine, 
    SUM(orderValue) totalOrderValue,
    GROUPING(orderYear),
    GROUPING(productLine)
FROM
    sales
GROUP BY 
    orderYear,
    productline
WITH ROLLUP;

关于subquery

SELECT 
    customerNumber, 
    checkNumber, 
    amount
FROM
    payments
WHERE
    amount > (SELECT 
            AVG(amount)
        FROM
            payments);

先subquery再外面的query

关于derived table:类似子查询

关于CTE(common table expression)

     这个其实我不太想写,这个呐mysql8.0开始支持,所以请注意版本

     A common table expression is a named temporary result set that exists only within the execution scope of a single SQL statement e.g.,SELECTINSERTUPDATE, or DELETE

WITH cte_name (column_list) AS (
    query
) 
SELECT * FROM cte_name;

CTE名 customers_in_usa,返回customerName和state,并在select中被使用

WITH customers_in_usa AS (
    SELECT 
        customerName, state
    FROM
        customers
    WHERE
        country = 'USA'
) SELECT 
    customerName
 FROM
    customers_in_usa
 WHERE
    state = 'CA'
 ORDER BY customerName;

再来一粒:两个CTE,第一个salesrep在第二个customer_salesrep中使用,第二个在select中被使用

WITH salesrep AS (
    SELECT 
        employeeNumber,
        CONCAT(firstName, ' ', lastName) AS salesrepName
    FROM
        employees
    WHERE
        jobTitle = 'Sales Rep'
),
customer_salesrep AS (
    SELECT 
        customerName, salesrepName
    FROM
        customers
            INNER JOIN
        salesrep ON employeeNumber = salesrepEmployeeNumber
)
SELECT 
    *
FROM
    customer_salesrep
ORDER BY customerName;

with 可以在 select 、 update 、 delete中使用

SELECT ... WHERE id IN (WITH ... SELECT ...);
SELECT * FROM (WITH ... SELECT ...) AS derived_table;
REPLACE ... WITH ... SELECT ...
EXPLAIN ... WITH ... SELECT ...
DECLARE CURSOR ... WITH ... SELECT ...

关于recursive CTE

     同样是从8.0开始支持

WITH RECURSIVE cte_name AS (
    initial_query  -- anchor member
    UNION ALL
    recursive_query -- recursive member that references to the CTE name
)
SELECT * FROM cte_name;

通过名字不知各位看官能否猜中一二:先执行initial_query,其结果会被recursive_query使用,循环执行直到返回空结果,最后使用union all 将查询结果加工处理,是不是有些绕,下面看一下栗子

限制条件:

  • recursive元素不能使用:max min  sum  avg count等函数
  • 不是group by 也不能order by,更不能limit,除此外就更别提distinct了(为了用连词,前后没有逻辑关系)
  • 递归元素只能在form中引用CTE名字一次,不能是子查询:the recursive member can only reference the CTE name once and in its FROM clause, not in any subquery

关于union

   和join的差异 这里就不说了

   和order by的火花一般的也不多了,下面应该也是一般的,但是写一下吧,不推荐使用,因为可能会令人一头雾水

SELECT 
    CONCAT(firstName,' ',lastName) fullname
FROM
    employees 
UNION SELECT 
    CONCAT(contactFirstName,' ',contactLastName)
FROM
    customers
ORDER BY 1;
这个1是什么意思?列的位置column position

关于intersect:mysql不支持的哦     

The INTERSECT operator compares the result sets of two queries and returns the distinct rows that are output by both queries.

如此这般mysql竟然不支持,当然我这里提到了,自然也找到了替换的方法:
   使用distinct 和 inner join

SELECT DISTINCT id FROM t1 INNER JOIN t2 USING(id);

   使用in

SELECT DISTINCT id FROM t1 WHERE id IN (SELECT id FROM t2);

关于minus:对了,没有mysql也是不支持的

     the MINUS operator is one of three set operators in the SQL standard that includes UNIONINTERSECT, and MINUS.

限制同intersect

看图大概知道什么意思了吧,去差集,如果有谁可以代替他的话,上left join 

SELECT 
    select_list
FROM 
    table1
LEFT JOIN table2 
    ON join_predicate
WHERE 
    table2.column_name IS NULL;

这一篇太长了些,就此写个上篇吧

https://www.mysqltutorial.org/basic-mysql-tutorial.aspx 这是部分

https://www.cnblogs.com/Little-Li/p/11152112.html 感谢分享

发布了431 篇原创文章 · 获赞 155 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/ma15732625261/article/details/104203653
今日推荐