Solved

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

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

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 108

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to count occurrences of each item in an array.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now