2017-07-18 118 views
1
table :First 
.............................................................................. 
id | ros | table | column | stool | date | places| 
.............................................................................. 
1 12 5 6 a 2017-07-17 goa | 
2 12 5 6 b 2017-07-17 delhi| 
.............................................................................. 


table :second 

.......................................................................... 
id | ros | name | email | phone | stos|address |date | 
.................................................................................................... 
5  12 and [email protected] 394924673464 6 fddsfds 2017-07-03 | 
6  12 her  [email protected] 84838593894  6 fdafdfd 2017-07-04 | 
.................................................................................................... 


Query like I am Using: 
SELECT `p`.* FROM `first` `p` LEFT JOIN `second` as `st` ON `st`.`ros`=`p`.`ros` ORDER BY `id` DESC LIMIT 10; 

我的問題是爲什麼它返回4行而不是2? 它的resurning 4價值沒有在表中我應該需要使用不同或分組爲由?後加入數據重複mysql查詢

+0

你會需要要添加另一個連接條件來停止重複,但表中的任何內容都不能滿足此要求。 –

回答

2

嘗試此查詢:

SELECT p.* FROM first as p LEFT JOIN second as st ON st.site_id=p.site_id group by p.id ORDER BY p.id DESC LIMIT 10; 
+0

Thanx working .... – Arushi

2

它返回4列,因爲你的表有重複site_id

用這個改變你的查詢。這將返回你只有2行。

SELECT `p`.* FROM `first` `p` LEFT JOIN `second` as `st` ON `st`.`site_id`=`p`.`site_id` GROUP BY p.id ORDER BY `id` DESC LIMIT 10; 
+0

Thanx working .... – Arushi

+0

@Arushi歡迎 – Tomas

1

它返回4行,因爲這兩個表中的所有site_id都是11.使它成爲兩列,只需在您的sql語句中添加Group By即可。

恰似:

SELECT `p`.* FROM `first` `p` LEFT JOIN `second` as `st` ON 
`st`.`site_id`=`p`.`site_id` GROUP BY `p`.`id` ORDER BY `id` DESC LIMIT 10; 
+0

Thanx工作.... – Arushi

0

回答你的問題:「爲什麼」的是:這是正常的,因爲你是加盟的site_id和你的第一個表中的每一行有2個在第二個表匹配site_id's行,以便2 * 2 = 4

#first table 
+-----+---------+ 
| id | site_id | 
+-----+---------+ 
| 31 | 11 | 
+-----+---------+ 
| 32 | 11 | 
+-----+---------+ 
# second table 
+-----+---------+ 
| id | site_id | 
+-----+---------+ 
| 5 | 11 | 
+-----+---------+ 
| 6 | 11 | 
+-----+---------+ 

當您從第一臺到site_id第二表連接31你會得到2行(31 with 5, and 31 with 6)

而當你從第一個表連接32到第二個表上site_id你將得到2行(32 with 5, and 32with 6)

你需要對結果進行分組在身上作爲@Sonu Bamniya說