2
我使用的查詢:GROUP_CONCAT與加入導致雙記錄
SELECT `customers`.`customers_id`,
`customer_name`,
GROUP_CONCAT(customer_tags.customer_tag_name ORDER BY customer_tag_name SEPARATOR ', '),
''
FROM `customers`
LEFT OUTER JOIN `customer_tags_ids` ON `customer`.`customers_id` = `customer_tags_ids`.`customers_id`
LEFT OUTER JOIN `customer_tags` ON `customer_tags_ids`.`customer_tags_ids_id` = `customer_tags`.`customer_tags_id`
WHERE `customers`.`account_id` = 1
GROUP BY `customers`.`customers_id`
ORDER BY `customers`.`customers_id` desc, `customers`.`customer_name` asc
對下面的表
客戶
+-------------+---------------+
|customers_id | customer_name|
+-------------+---------------+
|1 | Customer 1 |
+-------------+---------------+
|2 | Customer 2 |
+-------------+---------------+
customer_persons
+-----------+-------------+------------+------------+
| persons_id| customers_id| firstname | lastname |
+-----------+-------------+------------+------------+
|1 | 1 | Mehmet | Yaman |
+-----------+-------------+------------+------------+
|2 | 1 | Zafer | Zorlu |
+-----------+-------------+------------+------------+
|3 | 2 | Serkan | Eryaman |
+-----------+-------------+------------+------------+
|4 | 2 | Nedim | Yaman |
+-----------+-------------+------------+------------+
個
customer_tags
+-------------------+--------------------+
|customer_tags_id | customer_tag_name |
+-------------------+--------------------+
|1 | Google |
+-------------------+--------------------+
|2 | Yahoo |
+-------------------+--------------------+
|3 | Aol |
+-------------------+--------------------+
|4 | Facebook |
+-------------------+--------------------+
customer_tags_ids
+--------------------+------------------+--------------+
|customer_tags_ids_id| customer_tags_id| customers_id |
+--------------------+------------------+--------------+
|1 | 1 | 1 |
+--------------------+------------------+--------------+
|2 | 1 | 2 |
+--------------------+------------------+--------------+
|3 | 2 | 1 |
+--------------------+------------------+--------------+
|4 | 2 | 2 |
+--------------------+------------------+--------------+
|5 | 3 | 2 |
+--------------------+------------------+--------------+
|6 | 4 | 2 |
+--------------------+------------------+--------------+
我需要得到的結果:
+-----------+--------------+-----------------------------+----------------+
|customer_id|customer_name |firstname + lastname | customer_tags |
+-----------+--------------+-----------------------------+----------------+
|1 | Customer 1 | Mehmet Yaman, Zafer Zorlu | Google, Yahoo |
+-----------+--------------+-----------------------------+----------------+
|2 | Customer 2 | Serkan Eryaman, Nedim Yaman | Google, Yahoo, |
| | | | Aol, Facebook |
+-----------+--------------+-----------------------------+----------------+
但是當我使用上面的查詢我得到如下:
| CUSTOMERS_ID | CUSTOMER_NAME | CUSTOMERNAME | CUSTOMERTAGS |
|--------------|---------------|--------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|
| 1 | Customer 1 | Mehmet Yaman, Zafer Zorlu, Mehmet Yaman, Zafer Zorlu | Google, Google, Yahoo, Yahoo |
| 2 | Customer 2 | Serkan Eryaman, Serkan Eryaman, Nedim Yaman, Nedim Yaman, Serkan Eryaman, Serkan Eryaman, Nedim Yaman, Nedim Yaman | Aol, Aol, Facebook, Facebook, Google, Google, Yahoo, Yahoo |
正如您所看到的客戶名稱和標籤重複。我該如何解決這個問題?
好的。我寫了..謝謝.. – 2015-03-03 13:53:09
@DemirAksa所以什麼是問題?哪些記錄翻倍? – Alex 2015-03-03 13:56:50