mysql left join 左鏈接查詢關聯n多張表

left join 左鏈接即以左表爲基準,顯示座標全部的行,右表與左表關聯的數據會顯示,不關聯的則不顯示。關鍵字爲left join on。 
**基本用法以下: html

select table a left join table b on a.id = b.ta_id**

注意:
其中on後面關聯的字段應該是同一字段(兩表關聯的外鍵) 
因爲以左表爲基準,左表一條記錄若是對應右表多條記錄,那查出的數據中右表的數據也只顯示一條,若是要都顯示,能夠用group_contact()將字段用逗號隔開顯示在一條記錄上。因此右表無論有幾張,若是和左表都是一對一關係,則沒問題,存在一對多關係時,須要必定的處理。spa


 

一、內鏈接:將兩個表中存在連結關係的字段符合鏈接條件的記錄造成記錄集.net

SELECT
    A. NAME,
    B. NAME
FROM
    A
INNER JOIN B ON A.id = B.id

code

SELECT
    A. NAME,
    B. NAME
FROM
    A,
    B
WHERE
    A.id = B.id

結果是同樣的(內鏈接的inner關鍵字可省略);htm

二、外鏈接:分爲左外鏈接和右外鏈接blog

左鏈接A、B表結果包括A的所有記錄和符合條件的B的記錄。get

右聯結A、B表的結果和左聯結B、A的結果是同樣的,也就是說:it

SELECT
    A. NAME,
    B. NAME
FROM
    A
LEFT JOIN B ON A.id = B.id

io

SELECT
    A. NAME,
    B. NAME
FROM
    B
RIGHT JOIN A ON B.id - A.id

 

執行後的結果是同樣的。table

三、全聯結

四、無聯結

五、三表聯結查詢

SELECT
    username,
    psw,
    gname,
    tel
FROM
    (
        t1
        LEFT JOIN t2 ON t1.t1_id = t2.t1_id
    )
LEFT JOIN t3 ON t1.t1_id = t3.t1_id

 

六、終極的三表聯結查詢

items:商品表,item_visit_stats:商品訪問表,item_trade_stats:商品銷售表

/*

Source Server         : localhost
Source Server Version : 50505
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50505
File Encoding         : 65001

Date: 2018-09-14 19:00:46
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for items
-- ----------------------------
DROP TABLE IF EXISTS `items`;
CREATE TABLE `items` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT '',
  `price` varchar(255) DEFAULT '',
  `nick` varchar(255) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品表';

-- ----------------------------
-- Table structure for item_trade_stats
-- ----------------------------
DROP TABLE IF EXISTS `item_trade_stats`;
CREATE TABLE `item_trade_stats` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `buyer_num` int(11) unsigned DEFAULT '0',
  `item_num` int(11) unsigned DEFAULT '0',
  `seller_nick` varchar(255) DEFAULT '',
  `business_day` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品銷售表';

-- ----------------------------
-- Table structure for item_visit_stats
-- ----------------------------
DROP TABLE IF EXISTS `item_visit_stats`;
CREATE TABLE `item_visit_stats` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_visits` varchar(255) NOT NULL,
  `business_day` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品訪問表';

 



 

SELECT
    i.id,
    i.title,
    SUM(ivs.user_visits) AS uv,
    its.item_num * i.price AS turnover
FROM
    (
        items AS i
        RIGHT JOIN item_visit_stats AS ivs ON i.id = ivs.id
    )
LEFT JOIN (
    SELECT
        id,
        SUM(item_num) AS item_num
    FROM
        item_trade_stats
    WHERE
        seller_nick = "XXXX"
    GROUP BY
        id
) AS its ON its.id = ivs.id
WHERE
    i.nick = "XXXX"
GROUP BY
    i.id
ORDER BY
    uv DESC

 

 

https://blog.csdn.net/chentaocba/article/details/7697825

寫的時候從外層往裏寫,一層一層left join,纔不容易出錯。

http://www.cnblogs.com/amyStart/p/5965472.html

相關文章
相關標籤/搜索