2010-10-08 272 views
0

這裏是我稍微擦過的SQL查詢(只是列/表/數據庫名稱)。我保持結構與生產相同。這是從我的MySQL日誌中獲取的查詢。SQL查詢不返回預期結果

SELECT  master.pk AS pk, 
      master.vendor_id AS vendorId, 
      master.vendor_text AS vendorName, 
      master.device_id AS deviceId, 
      master.device_did AS deviceDid, 
      master.device_text AS deviceName, 
      master.class_id AS classId, 
      master.class_text AS className, 
      master.oem_id AS oemId, 
      master.oem_did AS oemDid, 
      master.oem_text AS oemName, 
      master.oem_vendor_text AS oemVendor, 
      master.comment AS comment, 
      master.v1 AS v1, 
      master.v2 AS v2, 
      master.status AS status, 
      dev_mod_join.mod_text AS module 
FROM  master 
LEFT JOIN dev_mod_join 
     ON master.device_did = dev_mod_join.dev_id 
WHERE ( 
      master.device_text LIKE CONCAT("%", 'search term', "%") 
     OR master.vendor_text LIKE CONCAT("%", 'search term', "%") 
     OR master.oem_text LIKE CONCAT("%", 'search term', "%") 
     OR master.oem_vendor_text LIKE CONCAT("%", 'search term', "%") 
    ) 
GROUP BY master.vendor_text 
LIMIT  0, 25 

由於某些原因,這隻會返回每個供應商的一個結果。這很奇怪。每個供應商可以有多個設備。如果我搜索一個我認識的設備很多的供應商,它仍然只會爲該供應商返回一個設備(它也會返回其他供應商提供的其他設備,但其他設備與搜索字詞相匹配,但只能提供一個供應商)。

我錯過了任何東西,我認爲它與「OR」有關,但從我所知道的情況來看,情況並非如此。

一如既往的幫助表示讚賞。

編輯:OMG,我可能只是想通了。我使用GROUP BY而不是ORDER BY。

哇,一直在尋找這一段時間,像SO 5分鐘,我得到它

+2

那'GROUP BY'條款是在其他SQL口味非法的。我認爲你應該嘗試刪除它並向我們報告。 – bernie 2010-10-08 06:10:38

+0

這確實是它是什麼。謝謝。 – Tim 2010-10-08 06:19:28

回答

0

查詢似乎是聲音在大多數情況下,看起來很可疑的只有一部分是由該組。嘗試使用不同的select子句而不是group by。

像這樣:

SELECT DISTINCT  
      master.pk AS pk, 
      master.vendor_id AS vendorId, 
      master.vendor_text AS vendorName, 
      master.device_id AS deviceId, 
      master.device_did AS deviceDid, 
      master.device_text AS deviceName, 
      master.class_id AS classId, 
      master.class_text AS className, 
      master.oem_id AS oemId, 
      master.oem_did AS oemDid, 
      master.oem_text AS oemName, 
      master.oem_vendor_text AS oemVendor, 
      master.comment AS comment, 
      master.v1 AS v1, 
      master.v2 AS v2, 
      master.status AS status, 
      dev_mod_join.mod_text AS module 
FROM  master 
LEFT JOIN dev_mod_join 
     ON master.device_did = dev_mod_join.dev_id 
WHERE ( 
      master.device_text LIKE CONCAT("%", 'search term', "%") 
     OR master.vendor_text LIKE CONCAT("%", 'search term', "%") 
     OR master.oem_text LIKE CONCAT("%", 'search term', "%") 
     OR master.oem_vendor_text LIKE CONCAT("%", 'search term', "%") 
    ) 
+0

還不能接受,但這是100%。擺脫了GROUP BY,切換到ORDER BY並添加了DISTINCT。謝謝! – Tim 2010-10-08 06:20:12