• MariaDB Subquery


    MariaDB Subquery

    prod_id ==> order_num

    (jlive)[crashcourse]>SELECT order_num FROM orderitems WHERE prod_id = 'TNT2';

    +-----------+

    | order_num |

    +-----------+

    |     20005 |

    |     20007 |

    +-----------+

    2 rows in set (0.00 sec)

    order_num ==> cust_id

    (jlive)[crashcourse]>SELECT cust_id FROM orders WHERE order_num IN (20005,20007);

    +---------+

    | cust_id |

    +---------+

    |   10001 |

    |   10004 |

    +---------+

     

    2 rows in set (0.00 sec)

    cust_id ==> cust_name,cust_contact

    (jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers WHERE cust_id IN (10001,10004);

    +----------------+--------------+

    | cust_name      | cust_contact |

    +----------------+--------------+

    | Coyote Inc.    | Y Lee        |

    | Yosemite Place | Y Sam        |

    +----------------+--------------+

     

    2 rows in set (0.00 sec)



    (jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers WHERE cust_id IN (SELECT cust_id FROM orders WHERE order_num  IN (SELECT order_num FROM orderitems WHERE prod_id = 'TNT2'));

    +----------------+--------------+

    | cust_name      | cust_contact |

    +----------------+--------------+

    | Coyote Inc.    | Y Lee        |

    | Yosemite Place | Y Sam        |

    +----------------+--------------+

     

    2 rows in set (0.01 sec)

    (jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers, orders, orderitems WHERE customers.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num AND prod_id = 'TNT2';

    +----------------+--------------+

    | cust_name      | cust_contact |

    +----------------+--------------+

    | Coyote Inc.    | Y Lee        |

    | Yosemite Place | Y Sam        |

    +----------------+--------------+

     

    2 rows in set (0.00 sec)

    (jlive)[crashcourse]>SELECT cust_name, cust_contact FROM customers AS c, orders AS o, orderitems AS oi WHERE c.cust_id = o.cust_id AND oi.order_num = o.order_num AND prod_id = 'TNT2';

    +----------------+--------------+

    | cust_name      | cust_contact |

    +----------------+--------------+

    | Coyote Inc.    | Y Lee        |

    | Yosemite Place | Y Sam        |

    +----------------+--------------+

     

    2 rows in set (0.00 sec)



    子查询混合计算

    (jlive)[crashcourse]>SELECT COUNT(*) AS orders FROM orders WHERE cust_id = 10001;

    +--------+

    | orders |

    +--------+

        2 |

    +--------+

     

    1 row in set (0.00 sec)

    (jlive)[crashcourse]>SELECT cust_id, cust_name, cust_state, (SELECT COUNT(*) FROM orders WHERE orders.cust_id = customers.cust_id) AS orders FROM customers ORDER BY cust_name;

    +---------+----------------+------------+--------+

    | cust_id | cust_name      | cust_state | orders |

    +---------+----------------+------------+--------+

    |   10001 | Coyote Inc.    | MI             2 |

    |   10005 | E Fudd         | IL             1 |

    |   10002 | Mouse House    | OH             0 |

    |   10003 | Wascals        | IN             1 |

    |   10004 | Yosemite Place | AZ             1 |

    +---------+----------------+------------+--------+

     

    5 rows in set (0.00 sec)

  • 相关阅读:
    理解 QEMU/KVM 和 Ceph(2):QEMU 的 RBD 块驱动(block driver)
    Neutron VxLAN + Linux Bridge 环境中的网络 MTU
    理解 QEMU/KVM 和 Ceph(1):QEMU-KVM 和 Ceph RBD 的 缓存机制总结
    [译] 企业级 OpenStack 的六大需求(第 3 部分):弹性架构、全球交付
    [译] 企业级 OpenStack 的六大需求(第 2 部分):开放架构和混合云兼容
    [译] 企业级 OpenStack 的六大需求(第 1 部分):API 高可用、管理和安全
    Javascript中的Array(数组) 、{}(映射) 与JSON解析
    HTML中显示特殊字符,如尖括号 “<”,">"等等
    Ubuntu 12.04 安装配置 Apache2
    Python中函数的参数传递与可变长参数
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814185.html
Copyright © 2020-2023  润新知