tags:

views:

22

answers:

1

Here's a SQL query that I slightly scrubbed (just the column/table/database names). I kept the structure the same as production. This is a query taken from my MySQL log.

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

For some reason, this is only returning one result per vendor. That is odd. There can be several devices per vendor. If I search for a vendor that I know has a lot of devices, it will still return only one device for that vendor (it also returns other devices that have other vendors, but something else matches the search term, but it only gives one per vendor).

Anything I'm missing, I think it has something to do with the "OR"s, but from what I know, that shouldn't be the case.

Help is appreciated, as always.

EDIT: OMG, I may have just figured it out. I used GROUP BY instead of ORDER BY.

Wow, been looking at that for a while and like 5 minutes on SO and I get it

A: 

The query appears to be sound for the most part, the only part that looks suspicious is the group by. Try it with a distinct in select clause instead of group by.

Like this:

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', "%") 
      )
James
can't accept yet, but that's 100% it. got rid of GROUP BY, switched to ORDER BY and added DISTINCT. Thanks!
Tim