mySQL在右表上具有(无行)条件的左连接
我想要一张这样的桌子:mySQL在右表上具有(无行)条件的左连接,mysql,mysqli,Mysql,Mysqli,我想要一张这样的桌子: SHOPS +----+------------+ | id | shop_name | +----+------------+ ORDERBOOKINGS +-----+---------+-------------+------------+ | id | shop_id | grand_total | created_at | +-----+---------+-------------+------------+ SELECT COUNT(orde
SHOPS
+----+------------+
| id | shop_name |
+----+------------+
ORDERBOOKINGS
+-----+---------+-------------+------------+
| id | shop_id | grand_total | created_at |
+-----+---------+-------------+------------+
SELECT
COUNT(orderbookings.id),
COALESCE(SUM(orderbookings.grand_total), 0),
shops.shop_name
FROM `orderbookings`
RIHGT JOIN shops on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
条件是我有一个日期过滤器,它只返回指定日期之间的订单总数。我希望它返回所有的商店(如果在这些日期之间没有一些商店的订单,那么它应该返回那些总订单为0的商店)
注意:有些商店甚至可能在orderbookings表中没有条目
我尝试了以下操作,但未能返回表中的所有行:
+------------+--------------+--------------+
| shop_name | total_orders | total_amount |
+------------+--------------+--------------+
你知道我怎样才能做到这一点吗
谢谢。将查询中的
替换为和,并将左连接替换为右连接
:
SELECT COUNT(orderbookings.id),
SUM(orderbookings.grand_total),
shops.shop_name
FROM `orderbookings`
LEFT JOIN shops
on orderbookings.shop_id = shops.id
where orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
解释:
1) 如果你想得到所有的商店,你应该使用shops
作为主表,然后左连接orderbookings
,这里我使用右连接,因为你使用orderbookings
作为主表
2) 如果您在的where
中使用orderbookings
的列,则左联接将作为内部联接
最后,left join
解决方案如下:
SHOPS
+----+------------+
| id | shop_name |
+----+------------+
ORDERBOOKINGS
+-----+---------+-------------+------------+
| id | shop_id | grand_total | created_at |
+-----+---------+-------------+------------+
SELECT
COUNT(orderbookings.id),
COALESCE(SUM(orderbookings.grand_total), 0),
shops.shop_name
FROM `orderbookings`
RIHGT JOIN shops on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
将查询中的替换为和,并将左连接
替换为右连接
:
SELECT COUNT(orderbookings.id),
SUM(orderbookings.grand_total),
shops.shop_name
FROM `orderbookings`
LEFT JOIN shops
on orderbookings.shop_id = shops.id
where orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
解释:
1) 如果你想得到所有的商店,你应该使用shops
作为主表,然后左连接orderbookings
,这里我使用右连接,因为你使用orderbookings
作为主表
2) 如果您在的where
中使用orderbookings
的列,则左联接将作为内部联接
最后,left join
解决方案如下:
SHOPS
+----+------------+
| id | shop_name |
+----+------------+
ORDERBOOKINGS
+-----+---------+-------------+------------+
| id | shop_id | grand_total | created_at |
+-----+---------+-------------+------------+
SELECT
COUNT(orderbookings.id),
COALESCE(SUM(orderbookings.grand_total), 0),
shops.shop_name
FROM `orderbookings`
RIHGT JOIN shops on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
您要反转联接并添加一些IFNull:
SELECT
COUNT(orderbookings.id),
COALESCE(SUM(orderbookings.grand_total), 0),
shops.shop_name
FROM `shops `
LEFT JOIN orderbookings on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
您要反转联接并添加一些IFNull:
SELECT
COUNT(orderbookings.id),
COALESCE(SUM(orderbookings.grand_total), 0),
shops.shop_name
FROM `shops `
LEFT JOIN orderbookings on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02"
GROUP BY shops.id
已经试过了。仍然只返回orderbookings表中有条目的商店。离开所有其他商店。@nikhil.malik更新了我的答案,请再次检查。现在开始工作。Thanx。!已经试过了。仍然只返回orderbookings表中有条目的商店。离开所有其他商店。@nikhil.malik更新了我的答案,请再次检查。现在开始工作。Thanx。!您可能需要将WHERE
条件移动到ON
子句中。无帮助。仍然只返回orderbookings表中有订单的商店。离开所有其他商店。你想让我提供真实的数据吗?哎呀,连接语法有点生疏了。尝试右外连接。更新了答案。@Ilion使用了IFNULL并将Where to on子句移到了何处。Thanx guy您可能需要将WHERE
条件移动到ON
子句中。无帮助。仍然只返回orderbookings表中有订单的商店。离开所有其他商店。你想让我提供真实的数据吗?哎呀,连接语法有点生疏了。尝试右外连接。更新了答案。@Ilion使用了IFNULL并将Where to on子句移到了何处。Thanx伙计们