2012-04-18 128 views
0

嘿,我有兩個表我想要得到的綜合結果出來的兩種結構是這樣的MySQL查詢加入

TABLEA

id  user 
1   2 
2   2 
3   3 
4   2 
5   2 
6   3 

tableB的

id  credit 
1  45 
2  43 
3  23 
4  25 
5  26 

兩個表ID是autoincrement和tableA.user = tableB.id所以我試圖執行此查詢,但它返回空結果

sql_query("SELECT * from tableA join tableB ON tableA.user=tableB.id "); 
+0

是tableA.user列和tableB.id列之間的數據類型相同嗎? – Taryn 2012-04-18 17:18:48

+0

是的,看到幾個地方的列用戶來了tableA。如果你會計數,你會發現它是來4個地方,所以我想總結在這種情況下用戶說2在tableA中的意見。我的查詢是這樣的:SELECT sum(views)as num from tableA join tableB ON tableA.user = tableB.id where user ='2' – Steeve 2012-04-18 17:23:22

+0

您是否嘗試過直接對數據庫運行查詢?如果是這樣,它會在那裏產生結果嗎? btw SELECT *現在不被認爲是好的做法。指定您想要返回的列。 – liquorvicar 2012-04-18 17:56:28

回答

0

試試這個:

sql_query("SELECT * from tableA inner join tableB ON tableA.user=tableB.id where tableA.user=tableB.id"); 

這將要給你只有那些滿足記錄你的加盟條件。

+0

not working我試過 – Steeve 2012-04-18 17:54:47

+0

更新的代碼..看到這個 – Dhruvisha 2012-04-18 17:56:28

+0

哪裏是代碼 – Steeve 2012-04-18 18:02:02

0

你缺少*

SELECT * 
+0

對不起,它在那裏,我輸入了錯誤 – Steeve 2012-04-18 17:13:01

0

你錯過*選擇之間FROM

sql_query("SELECT * from tableA join tableB ON tableA.user=tableB.id "); 
+0

對不起,它在那裏,我輸入了錯誤 – Steeve 2012-04-18 17:13:20

+0

這是正常的。請標記爲已回答。 – 2012-04-18 17:16:28

+0

沒有它沒有回答我得到空白輸出請幫助。 – Steeve 2012-04-18 17:17:46