简体   繁体   中英

SQL Query Help for searching

I have 3 tables named "pictures", "pictag" and "tagging". There is a search box in which the user types in the name of the image that they want to search. When user types in this term I want it say name, description and image.

For example, if the user "tags" an image with 'sun' then this image does not get displayed. I have tried many things but think there might be something in my code which is stopping this. What sort of query can I use for this as the one I am trying to execute does not show an image when the "tag" is being searched for?

The query is:

$sql = "
    SELECT pictures.idpic, pictures.name, pictures.info, pictag.tagpicID, pictag.pictagName, tagging.IDimage, tagging.tagpicID 
    FROM pictures, pictag, tagging 
    WHERE pictures.idpic = '$searchvalueentered ' 
    AND pictag.tagpicID= '$searchvalueentered ' 
    AND tagging.tagpicID= '$searchvalueentered '";

I don't quite understand what you are doing and what the three tables are for but have you tried to use "OR" in your where clause instead of "AND". I've got a hunch that the problem lies there.

Try this.

$sql = "
    SELECT pictures.idpic, pictures.name, pictures.info, pictag.tagpicID, pictag.pictagName, tagging.IDimage, tagging.tagpicID 
    FROM pictures, pictag, tagging 
    WHERE pictures.idpic = '" . $searchvalueentered . "' 
    OR pictag.tagpicID = '" . $searchvalueentered . "' 
    OR tagging.tagpicID = '" . $searchvalueentered . "'
";

You should consider using prepared statements, by the way. This is highly unsafe.

$sql = "
    SELECT pictures.idpic, pictures.name, pictures.info, pictag.tagpicID, pictag.pictagName, tagging.IDimage, tagging.tagpicID 
    FROM pictures, pictag, tagging
    WHERE pictures.idpic = '$searchvalueentered ' 
    OR pictag.tagpicID= '$searchvalueentered ' 
    OR tagging.tagpicID= '$searchvalueentered '";

Will work but you actually want to join your tables or else you will be getting duplicate records in your result (maybe this is what you want).

Assuming that pictag and tagging are 1:1 relations of your pictures table, I would urge you to try this method instead:

SELECT pictures.idpic, pictures.name, pictures.info, pictag.tagpicID, pictag.pictagName, tagging.IDimage, tagging.tagpicID 
FROM pictures
LEFT JOIN pictag ON pictures.idpic = pictag.tagpicID
LEFT JOIN tagging ON pictures.idpic = tagging.tagpicID
WHERE pictures.idpic = '$searchvalueentered';

I can't test the query for you but I suspect this will produce expected results, given that all tables are related by the "idpic" key.

As David mentioned, you should try joining your tables to eliminate duplicates and to simplify the search, you can try..

SELECT 
    pictures.idpic, 
    pictures.name, 
    pictures.info, 
    pictag.tagpicID, 
    pictag.pictagName, 
    tagging.IDimage, 
    tagging.tagpicID 
FROM pictures
INNER JOIN pictag 
    ON pictures.idpic = pictag.tagpicID
INNER JOIN tagging 
    ON pictures.idpic = tagging.tagpicID
WHERE 
    CONCAT_WS(' ',pictures.idpic,pictag.tagpicID,tagging.tagpicID) LIKE %$searchvalueentered%

Note: not a very optimised solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM