在这种情况下,我有很多关系问题
表:customer_master
+---------+-----------+
| cust_id | cust_code |
+---------+-----------+
| 1 | S00333 |
+---------+-----------+
表:customer_supply_chain
+----------------+-----------+-------------------+
| supply_cust_id | cust_code | supplye_cust_code |
+----------------+-----------+-------------------+
| 1 | S00333 | HI00001 |
| 2 | S00333 | MA00010 |
+----------------+-----------+-------------------+
表:customer_vehicle
+----------------+-----------+------------------+
| cust_vehicl_id | cust_code | vehicle_model_id |
+----------------+-----------+------------------+
| 1 | S00333 | 161 |
| 2 | S00333 | 162 |
+----------------+-----------+------------------+
Mysql查询是:
SELECT
cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM customer_master cm
LEFT JOIN customer_supply_chain csc
ON csc.cust_code = cm.cust_code
LEFT JOIN customer_vehicle cv
ON cv.cust_code = cm.cust_code
WHERE cm.cust_code = 'S00333'
查询结果
+------------------+------------------+
| vehicle_model_id | supply_cust_code |
+------------------+------------------+
| 161 | HI00001 |
| 162 | HI00001 |
| 161 | MA00010 |
| 162 | MA00010 |
+------------------+------------------+
我们的愿望输出是
+------------------+------------------+
| vehicle_model_id | supply_cust_code |
+------------------+------------------+
| 161 | HI00001 |
| 162 | MA00010 |
+------------------+------------------+
supply_cust_code
和vehicle_model_id
,
我们尝试使用distinct和group by以及其他修复程序(在堆栈溢出中显示)但不能正常工作,
如果我们使用group by,那么我们无法获取所有数据而我们不想使用group_concat
,我们无法解决问题,
我们只是尝试获得所有行但不重复
答案 0 :(得分:0)
我认为你需要这样的东西:
SELECT
cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM (
SELECT supply_cust_code, @i := @i + 1 AS pos
FROM (
SELECT csc.supply_cust_code
FROM customer_supply_chain csc
WHERE csc.cust_code = 'S00333') t,
(select @i := 0) r ) csc
LEFT JOIN (
SELECT vehicle_model_id, @j := @j + 1 AS pos
FROM (
SELECT cv.vehicle_model_id
FROM customer_vehicle cv
WHERE cv.cust_code = 'S00333') t,
(select @j := 0) r ) cv ON cv.pos = csc.pos
但是在你确切地说明你需要什么之前,你不可能在这里获得帮助。
答案 1 :(得分:-1)
我不知道您为什么会收到这些重复项,因为示例数据不支持它,但您可以尝试使用SELECT DISTINCT
:
SELECT DISTINCT cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM ...
或者您可以尝试对模型ID进行分组并提供客户代码:
SELECT cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM customer_master cm
LEFT JOIN customer_supply_chain csc
ON csc.cust_code = cm.cust_code
LEFT JOIN customer_vehicle cv
ON cv.cust_code = cm.cust_code
WHERE cm.cust_code = 'S00333'
GROUP BY cv.vehicle_model_id,
csc.supply_cust_code