Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is my wamp get_include_path() wrong? 2 21
Help with PHP 13 27
IP 10.0.1.2 / 255.0.0.0 61 56
Reference key in foreach loop 4 21
This article discusses four methods for overlaying images in a container on a web page
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

837 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