Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to bring back an column thats in a sub query using php and mysql

Posted on 2013-01-15
5
Medium Priority
?
331 Views
Last Modified: 2013-01-15
Hi

I have a php script with a query that  brings me back all books from the books table whos book date meets a criteria (date matches and books.uid <>1). The query worked fine, however when i added the available.AvailableDates it throws an error.
I want to bring the book date back too but as the table that holds the book date is in the subquery it cant find it. Below is the query with the available table and field added and the error:

// get all books from books table
$result = mysql_query("SELECT books.uid, book.name, book.description, available.AvailableDates
FROM books
WHERE EXISTS (SELECT 0 FROM available A1, available A2
WHERE A1.uid =1 AND A2.uid = books.uid
AND A1.AvailableDates = A2.AvailableDates
AND books.uid <>1)") or die(mysql_error());

error:
Unknown column 'available.AvailableDates' in 'field list'

How can i pull the availabledates column into the select statement?
0
Comment
Question by:wilko100
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38777683
try this

$result = mysql_query("SELECT books.uid, book.name, book.description, X.AvailableDates
FROM books
inner join (
SELECT A1.uid, A1.AvailableDates FROM available A1, available A2
WHERE A1.uid =1
AND A1.AvailableDates = A2.AvailableDates
) X on X.uid = books.uid
WHERE EXISTS (
AND books.uid <>1)") or die(mysql_error());
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38777762
Maybe this?

FROM books, available
0
 

Author Comment

by:wilko100
ID: 38777851
Hi
Tahnks for the quick response,  query by  pratima_mcs throws an error:

error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND books.uid <>1)' at line 8

but Ray_Paseur workied by simply added availble.

However its now raised another issue, im now returning all the books with that date more than once.
So for example, book 1 has 2 dates, book 2 matches those dates 2 dates, but the quey brings back book 2 four times. I want to see the book 2 dates 4 times but not the book name 4 times
I suspect i need some sort of destinct on the book but not the dates (i want to see all the dates for the book but just return book 2 once)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38777868
Please post the CREATE TABLE statements for the tables you want to query, thanks.
0
 

Author Closing Comment

by:wilko100
ID: 38780065
Thanks for the response, theres a few things i need to sort furst but your first is answer is spot on d-0-b
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question