Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 dynamically set the form action using jQuery.

932 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

9 Experts available now in Live!

Get 1:1 Help Now