zoukankan      html  css  js  c++  java
  • MySQL多表联合查询

    MySQL这方面的资料比较少,手边的项目用到了多表的联合查询,干脆备忘下来。

    select a.*,b.*,c.* from a  INNER JOIN  b  ON a.cid=b.cid  INNER JOIN c ON c.cid=a.cid  where  a.cid=2 and a.id =3
    SELECT e_link.link_id, e_link.l_name, e_link.url, e_link.pic_url, e_link.order_id AS l_order_id, e_link.font_color, 
    e_linkclass.name, e_linkclass.order_id AS c_order_id
    FROM  `e_link` 
    JOIN  `e_linkclass` ON e_link.c_id = e_linkclass.id
    AND e_link.c_id =1
    LIMIT 0 , 30
    

    INNER JOIN 运算 组合两个表中的记录,只要在公共字段之中有相符的值。 语法

    FROM table1 INNER JOIN table2 ON table1.field1 compopr table2.field2
    

    INNER JOIN 运算可分为以下几个部分: 部分 说明 table1, table2 记录被组合的表的名称。 field1, field2 被联接的字段的名称。若它们不是由数字构成的,则这些字段必须为相同的数据类型并包含同类数据,但它们无须具有相同的名称。 compopr 任何的关系比较运算子:"=," " SELECT CategoryName, ProductName FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID;在上面的示例中,类标识符是已被联接的字段,但是它并不包含在查询输出中,因它并非被包含在 SELECT 语句之中。在这个示例中,若要包含联接字段,将字段名包含在 SELECT 语句中, Categories.CategoryID. 也可以使用下列语法,在一个 JOIN 语句中链接多个 ON 子句:

    SELECT fields
    FROM table1 INNER JOIN table2 
    ON table1.field1 compopr table2.field1 AND 
    ON table1.field2 compopr table2.field2) OR 
    ON table1.field3 compopr table2.field3)];
    

    也可以使用下列语法,嵌套 JOIN 语句:

    SELECT fields
    FROM table1 INNER JOIN 
    (table2 INNER JOIN [( ]table3 
    [INNER JOIN [( ]tablex [INNER JOIN ...)]
    ON table3.field3 compopr tablex.fieldx)] 
    ON table2.field2 compopr table3.field3) 
    ON table1.field1 compopr table2.field2;
    

    在一个 INNER JOIN 之中,可以嵌套 LEFT JOIN 或 RIGHT JOIN,但是在 LEFT JOIN 或 RIGHT JOIN 中不能嵌套 INNER JOIN。

  • 相关阅读:
    编写测试类实现并发访问固定URL(亲测能用!!!)
    java项目添加log4j打印日志+转换系统时间
    springboot项目没错,但就是报红叉
    我想查看数据库名,输入命令:select name from v$database;为什么会说表和视图不存在
    DRUID连接池的实用 配置详解+使用方法+监控方式(太强大了!!!)
    Druid连接池 属性说明
    springBoot2.2.0+mybatis-xml文件方式+Oracle11g+jsp页面,实现简单的CRUD
    s5-12 RIP
    s5-12 RIP
    s5-13 RIP 为什么会 衰败
  • 原文地址:https://www.cnblogs.com/endige/p/2418704.html
Copyright © 2011-2022 走看看