mysql左联查询时 右表有多条关联左表同一记录的信息,只要有右表中有条件不符合,就不显示该所有左表记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql左联查询时 右表有多条关联左表同一记录的信息,只要有右表中有条件不符合,就不显示该所有左表记录相关的知识,希望对你有一定的参考价值。

左表
CREATE TABLE `roominfo` (
`roomID` int(11) NOT NULL AUTO_INCREMENT,
`roomNum` int(11) NOT NULL,
`classID` int(11) NOT NULL,
`available` tinyint(1) NOT NULL DEFAULT '1',
`used` tinyint(1) NOT NULL DEFAULT '0',
`booked` tinyint(1) NOT NULL,
`mark` varchar(50) DEFAULT NULL,
PRIMARY KEY (`roomID`),
KEY `classID` (`classID`),
KEY `roomNum` (`roomNum`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of roominfo
-- ----------------------------
INSERT INTO `roominfo` VALUES ('1', '1', '1', '1', '0', '0', 'null');
INSERT INTO `roominfo` VALUES ('2', '2', '2', '1', '0', '0', 'mark');
INSERT INTO `roominfo` VALUES ('11', '101', '1', '1', '0', '1', 'mark');
INSERT INTO `roominfo` VALUES ('12', '102', '1', '1', '0', '0', '');
INSERT INTO `roominfo` VALUES ('13', '103', '2', '1', '0', '0', 'null');
INSERT INTO `roominfo` VALUES ('14', '104', '2', '1', '0', '0', '');
右表:
CREATE TABLE `orders` (
`orderID` int(11) NOT NULL AUTO_INCREMENT,
`userNickName` varchar(30) NOT NULL,
`roomNum` int(11) NOT NULL,
`bookInDate` date NOT NULL,
`bookOutDate` date NOT NULL,
`mark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`orderID`),
KEY `userNickName` (`userNickName`),
KEY `roomNum` (`roomNum`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`userNickName`) REFERENCES `customerinfo` (`nickName`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of orders
-- ----------------------------
INSERT INTO `orders` VALUES ('1', 'aa', '101', '2013-12-24', '2013-12-26', null);
INSERT INTO `orders` VALUES ('2', 'lee', '101', '2013-12-20', '2013-12-21', null);

查询:
SELECT
roominfo.roomNum,
roominfo.classID
FROM
roominfo
LEFT OUTER JOIN orders ON orders.roomNum = roominfo.roomNum
where roomInfo.available = 1 and roomInfo.used = 0 and (booked = 0 or
(orders.bookInDate > '2013-12-22' and orders.bookInDate > '2013-12-25') OR (orders.bookOutDate < '2013-12-22' and orders.bookOutDate < '2013-12-25'))

只要 时间有一条不符合,那么所有关于roomNum 101 的记录都不显示,
谢谢

参考技术A SELECT
roominfo.roomNum,
roominfo.classID
FROM
roominfo
LEFT OUTER JOIN orders ON orders.roomNum = roominfo.roomNum
where roomInfo.available = 1 and roomInfo.used = 0 and (booked = 0 or
(orders.bookInDate > '2013-12-22' and orders.bookInDate > '2013-12-25') AND (orders.bookOutDate < '2013-12-22' and orders.bookOutDate < '2013-12-25'))

mysql left join 左连接查询关联n多张表

left join 左连接即以左表为基准,显示坐标所有的行,右表与左表关联的数据会显示,不关联的则不显示。关键字为left join on。 
**基本用法如下: 

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

注意:
其中on后面关联的字段应该是同一字段(两表关联的外键) 
由于以左表为基准,左表一条记录如果对应右表多条记录,那查出的数据中右表的数据也只显示一条,如果要都显示,可以用group_contact()将字段用逗号隔开显示在一条记录上。所以右表不管有几张,如果和左表都是一对一关系,则没问题,存在一对多关系时,需要一定的处理。


 

1、内连接:将两个表中存在连结关系的字段符合连接条件的记录形成记录集

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

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

结果是一样的(内连接的inner关键字可省略);

2、外连接:分为左外连接和右外连接

左连接A、B表结果包括A的全部记录和符合条件的B的记录。

右联结A、B表的结果和左联结B、A的结果是一样的,也就是说:

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

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

 

执行后的结果是一样的。

3、全联结

4、无联结

5、三表联结查询

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

 

6、终极的三表联结查询

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




以上是关于mysql左联查询时 右表有多条关联左表同一记录的信息,只要有右表中有条件不符合,就不显示该所有左表记录的主要内容,如果未能解决你的问题,请参考以下文章

mysql left join 左连接查询关联n多张表

mysql left join 右表存在重复数据,怎么处理

MySql

8.3.7 - mysql 表之间关系

使用 CROSS APPLY 与 OUTER APPLY 连接查询

使用 CROSS APPLY 与 OUTER APPLY 连接查询