[英]Join 5 tables in mysql to show two SUM columns of two different tables in the same query
[英]Join 2 MySQL tables with different information in same query
我在3天的時間里搜索了此問題的解決方案,嘗試了數十個查詢,但到目前為止都無濟於事... :(
我有表users
並且從那里列出具有colum user_type = 2
到現在為止還挺好...
然后,我得到了表feedback
並且我想在同一查詢中輸入用戶列表和每個用戶所擁有的平均反饋(知道有些用戶沒有反饋,因此甚至沒有在反饋表中列出)。
到目前為止,返回的唯一查詢是:
SELECT DISTINCT u.user_id, u.company_name, u.post_code, u.about_company, avg(fd.rating) AS rating FROM users u LEFT JOIN feedback fd ON fd.vendor_id = u.user_id WHERE u.user_type=2 ORDER BY u.user_id ASC
現在,在“反饋”表上,目前只有一家公司提供反饋。 但是,此清單應返回所有具有user_type = 2列的用戶,該列約為20左右。 問題在於結果只有1家公司(當我僅列出公司時,這是列表中的第一個),但是連反饋的公司都沒有,因此“評級”列將分配給錯誤的公司。
太糟糕了...畢竟這是一團糟,我希望有人能理解我的需求並能為我提供幫助。 :)
編輯1
我的users
表具有以下字段:user_id,company_name,post_code,about_company
我的feedback
表具有以下字段:feedback_id,job_id,vendor_id(與表用戶的user_id相同),等級
編輯2
好吧,我試圖保持這個簡單! 我的表格中的字段比這些字段多得多,而我的查詢實際上要復雜得多,因為它會考慮緯度和經度來計算距離。 這是REAL查詢:
SELECT DISTINCT u.user_id, u.company_name, u.post_code, u.about_company, coalesce(avg(fd.rating),0) AS rating FROM users u LEFT OUTER JOIN feedback fd ON fd.vendor_id = u.user_id WHERE u.lat!='' AND u.longt!='' AND u.status!=0 AND u.user_type=2 AND SQRT( POW( 69.1 * ( u.lat - 38.7376772 ) , 2 ) + POW( 69.1 * ( -9.1269717 - u.longt ) * COS( u.lat / 57.3 ) , 2 ) ) < 100 ORDER BY u.user_id ASC
聽起來您需要左外部聯接。 我假設您的架構看起來像這樣:
create table users (user_id int, user_type int);
create table feedback (feedback_id int, user_id int, feedback_value int);
SELECT user_id, avg(feedback_value)
FROM users
LEFT OUTER JOIN feedback USING(user_id)
WHERE users.user_type=2
GROUP BY user_id
可能想在此處合並以返回零與空值
SELECT user_id, coalesce(avg(feedback_value),0) as average_feedback
FROM users
LEFT OUTER JOIN feedback USING(user_id)
WHERE users.user_type=2
GROUP BY user_id
這是它工作的一個例子。
mysql> create database test_pedro;
Query OK, 1 row affected (0.00 sec)
mysql> use test_pedro;
Database changed
mysql> create table users (user_id int, user_type int);
Query OK, 0 rows affected (0.02 sec)
mysql> create table feedback (feedback_id int, user_id int, feedback_value int);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into users values (1,2), (2,2), (3,3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into feedback values (1,1,4), (2,1,19);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT user_id, coalesce(avg(feedback_value),0) as average_feedback
FROM users
LEFT OUTER JOIN feedback USING(user_id)
WHERE users.user_type=2
GROUP BY user_id;
+---------+------------------+
| user_id | average_feedback |
+---------+------------------+
| 1 | 11.5000 |
| 2 | 0.0000 |
+---------+------------------+
2 rows in set (0.00 sec)
mysql>
這是預期的輸出嗎?
這是您的更新查詢,帶有一些mod。 專門刪除DISTINCT並添加GROUP BY以使用AVG()函數
SELECT u.user_id,
u.company_name,
u.post_code,
u.about_company,
Coalesce(Avg(fd.rating), 0) AS rating
FROM users u
LEFT OUTER JOIN feedback fd
ON fd.vendor_id = u.user_id
WHERE u.lat != ''
AND u.longt != ''
AND u.status != 0
AND u.user_type = 2
AND Sqrt(Pow( 69.1 * ( u.lat - 38.7376772 ), 2 ) + Pow( 69.1 * ( -9.1269717 - u.longt ) * Cos( u.lat / 57.3 ), 2)) < 100
GROUP BY u.user_id,
u.company_name,
u.post_code,
u.about_company
ORDER BY u.user_id ASC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.