Long nested SELECT statement - Please help

Codes here !

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Tue Mar 12, 2013 11:28 am

Long nested SELECT statement - Please help

Postby dbewick123 » Wed Mar 13, 2013 10:31 am

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

Code: Select all

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



php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Long nested SELECT statement - Please help

Postby johnj » Thu Mar 14, 2013 8:01 pm

One way to do this will be to assign each of the SQL queries to variables, print it using php and then copy that SQL and take to a mySQL ui like phpmyadmin and run the query there to see if the query is doing what it should do.

Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests