I have a website that is running a query off to Mysql when a search term is entered. I built a sql statement like this
SELECT * FROM catelogue
WHERE `DEPARTMENT` LIKE 'MUSIC'
AND ( `ARTIST` LIKE '%$theValue%' OR `TITLE` LIKE '%$theValue%' OR
`CAT NO.` IN
(SELECT `TRACKLISTING CAT NO.`
FROM tracklisting
WHERE `ARTIST` LIKE '%$theValue%' OR `TRACK TITLE` LIKE '%$theValue%'))
ORDER BY `RELEASEDATE` DESC
It worked great on wamp locally when testing. Now I have uploaded the site to my host and the query seems to be crashing or slowing it down that it doesn’t even load the page.
I redesigned the query to this
SELECT `ARTIST` , `TITLE` , `CAT NO.` , `FORMAT` , `DEPARTMENT` , `SELL`,`IMAGE PATH`
FROM catelogue
WHERE `DEPARTMENT` LIKE 'MUSIC'
AND ( `ARTIST` LIKE '%$theValue%' OR `TITLE` LIKE '%$theValue%')
ORDER BY `RELEASEDATE` DESC"
As this stands it works great on my host now but I can’t search my tracklisting table. I need to know how to change IN to something that works faster.
The catelogue and tracklisting table are linked by the CAT NO.
Thanks
EDIT
A BIG THANKS TO EVERYONE THAT HELPED
Now onto some bad news. This is the code I’m using now. It doesn’t return any errors but it also doesn’t return any results either
SELECT c.`ARTIST` , c.`TITLE` , c.`CAT NO.` , c.`FORMAT` , c.`DEPARTMENT` , c.`SELL`
FROM catelogue c
LEFT JOIN tracklisting t
ON t.`TRACKLISTING CAT NO.` = c.`CAT NO.`
WHERE 'DEPARTMENT' = 'MUSIC'
AND (( c.`ARTIST` LIKE '%$theValue%' OR c.`TITLE` LIKE '%$theValue%')
OR
(t.`ARTIST` LIKE '%$theValue%' OR t.`TRACK TITLE` LIKE '%$theValue%'))
The tracklisting table doesn’t have all the tracklistings for every entry in the catelogue table. Would that make a difference?
I know in ms access you can setup relationships with tables, but I have been unable to figure out if this can be done in phpmyadmin, do I have to setup a relation between the tables for this to work?
Many Thanks
Well, I don’t really do PHP so some of your syntax is a little foreign to me but sql is sql and I’ve just copied the bits I’m not used to.
This should do what you want:
By the way, the field naming in this database is just terrifying IMHO. It would also probably help if you could do some form of full text indexing so that these “contains” LIKE comparisons don’t cause you to do full table scans every time. Just a thought.