CodingARgh MySQL

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  steve  
 To:  ALL
31872.1 
:C

I have a query

quote:
SELECT DISTINCT shops.* FROM shops, shops_products, shops_products_link WHERE ( ( ( shops_products.product LIKE '%photography%') AND (shops_products.sid = shops.sid) ) OR ( (shops_products_link.child_title LIKE '%photography%') AND ( shops_products.product = shops_products_link.parent_title ) AND (shops_products.sid = shops.sid) ) ) ORDER BY shops.shopname LIMIT 0, 18


But what it basically is is;

quote:
SELECT stuff FROM tables WHERE (one condition) OR (another condition)


Except...

To include a table in one of the conditions, I have to have it in the FROM statement.

BUT :(

The first conditions looks at one table, and second at another (seeing if it matches in either of them).

So if the first condition is true, but the second isn't - it won't return anything :(

AND I DO NOT UNDERSTAND. Even worse I'm not sure how to even explain what I am doing, but hope someone with good-geek-eyes can look at it and help me :'S

(Had to make them quotes as the code tags make them scroll)

edit: The simple solution would be to just do two queries and combine the results. BUT that is very messy :(

0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  steve     
31872.2 In reply to 31872.1 

without bothering to think about the sql ....

 

you might want to look at UNION - lets you do 2 queries as one, sort of!

0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  steve     
31872.3 In reply to 31872.1 
or this .....

sql code:
SELECT DISTINCT s.* 
FROM shops s 
LEFT JOIN shops_products sp ON s.sid=sp.sid
LEFT JOIN shops_products_link spl ON sp.product=spl.parent_title
WHERE (( shops_products.product LIKE '%photography%') 
OR (shops_products_link.child_title LIKE '%photography%'))
ORDER BY shops.shopname LIMIT 0, 18
0/0
 Reply   Quote More 

 From:  steve  
 To:  Ben (BENLUMLEY)     
31872.4 In reply to 31872.3 
THANKYOU BEN!

I played with union and made it work so I will stick with that :$ I have a script putting together the query, for multiple-keyword-searches it gets a bit messy :O

quote:
(SELECT DISTINCT shops.* FROM shops, shops_products WHERE (shops_products.product LIKE '%35mm%') AND (shops_products.sid = shops.sid) OR (shops_products.product LIKE '%photography%') AND (shops_products.sid = shops.sid)) UNION (SELECT DISTINCT shops.* FROM shops, shops_products, shops_products_link WHERE ( (shops_products_link.child_title LIKE '%35mm%') AND ( shops_products.product = shops_products_link.parent_title ) AND (shops_products.sid = shops.sid) ) OR ( (shops_products_link.child_title LIKE '%photography%') AND ( shops_products.product = shops_products_link.parent_title ) AND (shops_products.sid = shops.sid) )) ORDER BY shopname LIMIT 0, 18

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  steve     
31872.5 In reply to 31872.4 
You have:
code:
WHERE (shops_products.product LIKE '%35mm%') AND (shops_products.sid = shops.sid)
OR (shops_products.product LIKE '%photography%') AND (shops_products.sid = shops.sid)


You want:
code:
WHERE (shops_products.sid = shops.sid)
AND ((shops_products.product LIKE '%35mm%') OR (shops_products.product LIKE '%photography%'))


(unless I'm missing something?)
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  steve     
31872.6 In reply to 31872.4 

use the joins :@

 

use the joins :@

0/0
 Reply   Quote More 

 From:  steve  
 To:  Peter (BOUGHTONP)     
31872.7 In reply to 31872.5 
Aha yes! The query is made within a loop and I hadn't noticed that - it is now fixed thankyou (aww)

Ben: NO :@

0/0
 Reply   Quote More 

Reply to All    
 

1–7

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats