2015-11-22 113 views
2

我有這兩個表:SQL查詢新手問題

表1

Itemid  Username   Rate  Opinion  
0944947  pooh_sweety_70  3.2   Negative 
0468569  pooh_sweety_70     Positive 
0411008  liamvdheuvel  2.1   Negative 
0468569  liamvdheuvel  3.4   Negative 
1375666  liamvdheuvel  1.5   Negative 
0903747  liamvdheuvel  5.5   Negative 
0412142  rzajac    4.8   Negative 
2267998  rzajac    9.2   Positive 
1074638  rzajac    7.4   Positive 
1182345  rzajac    6.1   Negative 
0468569  rzajac    3.3   Negative 
1856010  michaelsmith01  9.9   Positive 
1486217  michaelsmith01     Positive 
1300854  michaelsmith01  2.4   Negative 
2267998  pooh_sweety_70  8.4   Positive 
2911666  aswilliams40  8.7   Positive 
1074638  aswilliams40  9.4   Positive 
0468569  aswilliams40  8.2   Positive 

表2

Username   Password 
aswilliams40  pantsftw 
liamvdheuvel  qwascoolzuiop 
michaelsmith01  ilovejessica 
rzajac    toocoolforscool 
pooh_sweety_70  legendarymythic 

而且我想找到其密碼包含單詞「酷」的用戶某個地方,並且該用戶還必須始終對項目留下否定意見,如果他們對該項目進行了評級。未分級的項目不重要。

我試圖把查詢在一起,但它並沒有真正的工作:/

SELECT Username FROM Table1, Table2 
    WHERE Table1.Username = Table2.Username AND 
    Table2.Password LIKE '%cool%' AND 
    Table1.Rate IS NOT NULL AND 
    Table1.Opinion = 'Negative'; 

所以基本上我唯一想要的輸出是「liamvdheuvel」

+1

做什麼你的意思是不起作用? –

+0

'WHERE Table2.Username = Table2.Username AND'?將一個表更改爲Table1 – c4pricorn

+0

它給出了以下錯誤:字段列表中的列'用戶名'不明確 –

回答

1
SELECT Table2.Username 
FROM Table1, 
     Table2 
WHERE Table1.Username = Table2.Username 
     AND Table2.Password LIKE '%cool%' 
     AND Table1.Rate IS NOT NULL 
     AND Table1.Opinion = 'Negative'; 

的問題是,你沒有指定你想要的表,以獲得 '用戶名'。

如果你不想得到重複的用戶名,添加DISTINCT

SELECT DISTINCT Table2.Username 
FROM Table1, 
     Table2 
WHERE Table1.Username = Table2.Username 
     AND Table2.Password LIKE '%cool%' 
     AND Table1.Rate IS NOT NULL 
     AND Table1.Opinion = 'Negative'; 

如果你不希望有消極和積極的意見的用戶,試試這個:

SELECT DISTINCT Table2.Username 
FROM Table1, 
     Table2 
WHERE Table1.Username = Table2.Username 
     AND Table2.Password LIKE '%cool%' 
     AND Table1.Rate IS NOT NULL 
     AND Table1.Opinion = 'Negative' 
     AND Table2.Username NOT IN (SELECT Table1.Username 
            FROM Table1 
            WHERE Table1.Opinion = 'Positive'); 
+1

感謝您的評論!我試過了,這個;它運行!但並不如預期的輸出,查詢返回:liamvdheuvel liamvdheuvel liamvdheuvel liamvdheuvel rzajac rzajac rzajac 而預期輸出是:liamvdheuvel –

+0

好吧,我知道爲什麼你的是,我會改變我的答案 –

+0

@ aurellb和rzajac也滿足了這個查詢,因爲他在密碼和負數中都有「酷」。 –

1
SELECT DISTINCT Table1.Username 
FROM Table1 
LEFT JOIN Table2 
ON table1.Username=table2.Username 
Where 
Table2.Password LIKE '%cool%' AND 
Table1.Rate IS NOT NULL AND 
Table1.Opinion = 'Negative'; 

爲了優化你需要刪除來自table1的用戶名(爲此表提供一個適當的名稱並將用戶ID添加到table2中(將其正確命名))。

Table2 
------ 

UserId **Username**   **Password** 
1   aswilliams40  pantsftw 
2   liamvdheuvel  qwascoolzuiop 
3   michaelsmith01  ilovejessica 
4   rzajac    toocoolforscool 
5   pooh_sweety_70  legendarymythic 

表1

**Itemid**  **UserID**   **Rate**  **Opinion**  
    0944947  5       3.2   Negative 
    0468569  5          Positive 
    0411008  2      2.1   Negative 
+0

感謝您的支持評論,但這仍然給我帶來的錯誤: 字段列表中的'用戶名'含糊不清 –

+0

@aurellb嘗試用我回答的查詢 –

+0

@aurell再次檢查 –