2017-09-16 68 views
2

嗨,大家好我是mySQL中的新成員,我在查詢時遇到問題。我試圖寫一些查詢,讓我從表中的所有記錄,如果我有兩個記錄具有相同的日期,我需要只有這兩個之間manual_selection = 1記錄。在MySQL中選擇所有具有不同和條件的行

所以結果應該是我的所有記錄除了ID表= 1401和id = 1549

my table

我想結合我怎麼能得到這個記錄是這樣的:

SELECT * FROM project.score WHERE project_id = 358 
AND crawled_at IN(SELECT crawled_at FROM project.score WHERE project_id = 358 
AND manual_selection = 1 GROUP BY crawled_at) 
ORDER BY crawled_at;  

SELECT * FROM project.score WHERE project_id = 358 
GROUP BY crawled_at HAVING manual_selection = 1; 

但我所有的方式總是隻與manual_selection = 1的行我有ent idea我怎麼能在manual_selection = 1的情況下使用重複的「crawled_at」來區分行。有人可以幫我嗎?

+0

你可以使用的組合'SELECT DISTINCT(場)'和'ORDER BY' – Manav

+0

但我需要的條件如果我有兩個具有相同日期的行,我只有一個manual_selection = 1,我不知道該如何做到這一點 – LukeKov

+0

它會更容易做到這一點,使用像'php'這樣的腳本而不是試圖製作一個mysql查詢 – Manav

回答

1

試試這個:

select main.id, main.project_id, main.crawled_at, main.score, main.manual_selection 
from dcdashboard.moz_optimization_keywords as main 
left join dcdashboard.moz_optimization_keywords as non_manual_selection on non_manual_selection.crawled_at = main.crawled_at and non_manual_selection.manual_selection != 1 
group by main.crawled_at; 

結果的數據與來自問題集:

+------+------------+---------------------+-------+------------------+ 
| id | project_id | crawled_at   | score | manual_selection | 
+------+------------+---------------------+-------+------------------+ 
| 807 |  360 | 2016-02-06 00:00:00 | 76 |    0 | 
| 1001 |  360 | 2016-02-20 00:00:00 | 76 |    0 | 
| 223 |  360 | 2016-11-28 00:00:00 | 76 |    0 | 
| 224 |  360 | 2016-12-05 00:00:00 | 76 |    0 | 
| 670 |  360 | 2016-12-19 00:00:00 | 76 |    0 | 
| 1164 |  360 | 2017-04-19 00:00:00 | 78 |    1 | 
| 1400 |  360 | 2017-09-13 00:00:00 | 96 |    1 | 
| 1548 |  360 | 2017-09-15 00:00:00 | 96 |    1 | 
+------+------------+---------------------+-------+------------------+ 
8 rows in set (0.00 sec) 
+0

這項工作很好。當我有兩個具有相同日期的記錄時,這個查詢返回兩個記錄中的一個,但是使用manual_selection = 0 – LukeKov

+0

對於我來說,它會返回一個manual_selection = 1的記錄(如果有兩個記錄具有相同的日期)。至少,你的數據集是這樣的。請再檢查一次。 –

+0

對不起,但是當我測試它時,我的結果與你的相同 – LukeKov