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
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 :/ ?? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.