Jump to content

Long nested SELECT statement


dbewick123

Recommended Posts

Ok, I am trying to do some camparisons in my tables and return results based on weather values match... i'll post the code then explain it, I think it will be easier that way

 

/* the below method works and returns all the desired tags */

 

 

$tags = mysql_query("SELECT `tag1`, `tag2`, `tag3`, `tag4`, `tag5` FROM `books` WHERE `book_id` IN (SELECT `book_id` FROM `userBooks` WHERE `user_id` = '$sessionUserId')");
 
/* this is the method i am struggling with, I am trying to SELECT the book info (auther etc.) from the books table WHERE the $tags (from previous query, match any tags of other books in the `books` table. */
 
$recommended =mysql_query("SELECT `auther`, `title`, `isbn` FROM `books`  
WHERE `$tags` = (SELECT `tag1`, `tag2`, `tag3`, `tag4`, `tag5` from `books` WHERE  `book_id` NOT IN (SELECT `book_id` FROM `userBooks` WHERE `user_id` = '$sessionUserId'))");
 

the way i interpret this query is that it will SELECT the `auther` `title` etc. from the books table where the $tags = the tags in the `books` table WHERE book_id is not already in the `userBooks` table against that `user_id` - this obviously isnt happending because nothing is being returned.

 

Please can someone help/advise me with this?

 

Thanks 

 

Dbewick123

Link to comment
https://forums.phpfreaks.com/topic/275622-long-nested-select-statement/
Share on other sites

Correctly normalize those tags into another table then come back and we can help you.

I've been reading up on normalizing databases and 1NF, 2NF and 3NF to a point where im fairly sure i understand it, however i'm unclear as to what you are telling me to do.... are you saying that i should put these tags into a seperate database table with the specific user id as a primary key :/ ?? 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.