dbewick123 Posted March 13, 2013 Share Posted March 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275622-long-nested-select-statement/ Share on other sites More sharing options...
Barand Posted March 13, 2013 Share Posted March 13, 2013 Correctly normalize those tags into another table then come back and we can help you. Quote Link to comment https://forums.phpfreaks.com/topic/275622-long-nested-select-statement/#findComment-1418479 Share on other sites More sharing options...
dbewick123 Posted March 14, 2013 Author Share Posted March 14, 2013 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 :/ ?? Quote Link to comment https://forums.phpfreaks.com/topic/275622-long-nested-select-statement/#findComment-1418573 Share on other sites More sharing options...
Barand Posted March 14, 2013 Share Posted March 14, 2013 As the tags belong to a book, why would you use the the user_id? Table book_tag: book_id INT tag_id INT PRIMARY KEY (book_id, tag_id) Table tag: tag_id INT PK tag_value VARCHAR(nn) Quote Link to comment https://forums.phpfreaks.com/topic/275622-long-nested-select-statement/#findComment-1418601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.