美文网首页
MySQL从入门到掌握

MySQL从入门到掌握

作者: 一颗西蓝花_ | 来源:发表于2020-05-28 07:08 被阅读0次

    一、安装步骤

    SELECT * 
    FROM orders
    WHERE shipper_id is NULL
    
    返回结果是一个表格 image.png

    2.6 The ORDERS BY Clause

    SELECT * 
    FROM customers
    ORDER BY first_name
    
    image.png

    我们也可以倒序排列

    SELECT * 
    FROM customers
    ORDER BY first_name DESC
    

    我们也可以,先按一个字段排序,再按一个字段排序

    SELECT * 
    FROM customers
    ORDER BY state, first_name
    
    当我们从一个表格中,提取列时,我们可以用并非被提取的列,来做sort image.png

    我们也可以在选择时候,重新为表格插入一列,并且为之命名

    SELECT first_name, last_name, 10 AS points
    FROM customers
    ORDER BY point, first_name
    

    一个比较复杂一点的例子,用到了的知识点有 Alias、WHERE、ORDER BY、DESC

    SELECT *, quantity * unit_price AS total_price
    FROM order_items
    WHERE order_id = 2
    ORDER BY total_price DESC
    

    2.7 The Limit Clause

    • 我们只想返回表格的前3行
    SELECT * 
    FROM customers
    LIMIT 3
    -- page 1:1- 3
    -- page 2: 4-6
    -- page 3: 7-9
    
    • 我们只想每页返回3个item
    SELECT *
    FROM customers
    LIMIT 6, 3
    

    这个是跳过开始的6个,只返回最后的3个。

    2.8 Inner Joins

    SELECT * 
    FROM orders
    INNER JOIN customers
        ON oders.customer_id = customers.cust_id
    
    SELECT order_id, orders.customer_id, first_name, last_name
    FROM orders
    JOIN customers 
        ON orders.customer_id = customers.customer_id
    

    如果说,认为需要打多个orders或者customers,过于麻烦,我们可以为orders和customers取别名,比如如下

    SELECT order_id, o.customer_id, first_name, last_name
    FROM orders o
    JOIN customers c
        ON o.customer_id = c.customer_id
    

    2.9 Joining Across Databases

    2.10 Self Joins

    USE sql_hr; -- 选择特定的数据库
    SELECT * 
    FROM employees e
    JOIN employees m
        ON e.reports_to = m.employee_id
    

    2.10 Joining Multiple Tables

    相关文章

      网友评论

          本文标题:MySQL从入门到掌握

          本文链接:https://www.haomeiwen.com/subject/wconahtx.html