Solved

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

Posted on 2013-01-15
5
305 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 109

Accepted Solution

by:
Ray Paseur earned 500 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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 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