473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

PHP 5 / MySQL 5 subquery problem

Kind readers,

I am currently developing an app with PHP 5 & MySql 5.

The following select statement works as expected from
the MySql command line but produces an error when run from PHP.

SELECT pkey, Name FROM tracks WHERE artist="Rolling Stones"
AND pkey NOT IN (SELECT Song FROM requests WHERE client="jim")

The error produced by PHP is

"Query failed: 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 ')' at line 1"

I've done a workaround by substituting the pkey values in
the parentheses in place of the subquery SELECT statement,
but I can't figure out why the above query does not work in
PHP but does in MySQL.

The versions of each app read as follows ...
>mysql -V
mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32)
>php -v
PHP 5.2.0-dev (cli) (built: Jul 19 2006 16:20:24)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

Any insight would be much appreciated.

Cheers
Jim
Oct 10 '06 #1
4 6225

Diogenes wrote:
Kind readers,

I am currently developing an app with PHP 5 & MySql 5.

The following select statement works as expected from
the MySql command line but produces an error when run from PHP.

SELECT pkey, Name FROM tracks WHERE artist="Rolling Stones"
AND pkey NOT IN (SELECT Song FROM requests WHERE client="jim")

The error produced by PHP is

"Query failed: 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 ')' at line 1"

I've done a workaround by substituting the pkey values in
the parentheses in place of the subquery SELECT statement,
but I can't figure out why the above query does not work in
PHP but does in MySQL.

The versions of each app read as follows ...
>mysql -V
mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32)
>php -v
PHP 5.2.0-dev (cli) (built: Jul 19 2006 16:20:24)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

Any insight would be much appreciated.

Cheers
Jim
It is almost always more efficient to use the LEFT JOIN alternative for
this task

Oct 11 '06 #2
Captain Paralytic wrote:
Diogenes wrote:
>>
SELECT pkey, Name FROM tracks WHERE artist="Rolling Stones"
AND pkey NOT IN (SELECT Song FROM requests WHERE client="jim")


It is almost always more efficient to use the LEFT JOIN alternative for
this task
I'm a little naive on SQL. Could you redo the above SELECT
statement with your suggested alternative? Then I'll try it
in PHP.

Cheers
Jim
Oct 11 '06 #3

Diogenes wrote:
Captain Paralytic wrote:
Diogenes wrote:
>
SELECT pkey, Name FROM tracks WHERE artist="Rolling Stones"
AND pkey NOT IN (SELECT Song FROM requests WHERE client="jim")

It is almost always more efficient to use the LEFT JOIN alternative for
this task

I'm a little naive on SQL. Could you redo the above SELECT
statement with your suggested alternative? Then I'll try it
in PHP.

Cheers
Jim
Without seeing the scheme and some sample data I can't be sure that
I've captured all the nuances of what you want, but try:

SELECT pkey, Name FROM tracks
LEFT JOIN requests ON tracks.pkey = requests.Song AND requests.client =
"jim"
WHERE tracks.artist="Rolling Stones" AND requests.Song IS NULL

Oct 12 '06 #4
Kudos to the Captain! That works beautifully both on the
command line and in PHP!

Thank you! Thank you! Thank you!

Cheers
Jim

Captain Paralytic wrote:
Diogenes wrote:
>>Captain Paralytic wrote:
>>>Diogenes wrote:

SELECT pkey, Name FROM tracks WHERE artist="Rolling Stones"
AND pkey NOT IN (SELECT Song FROM requests WHERE client="jim")

It is almost always more efficient to use the LEFT JOIN alternative for
this task

I'm a little naive on SQL. Could you redo the above SELECT
statement with your suggested alternative? Then I'll try it
in PHP.

Cheers
Jim


Without seeing the scheme and some sample data I can't be sure that
I've captured all the nuances of what you want, but try:

SELECT pkey, Name FROM tracks
LEFT JOIN requests ON tracks.pkey = requests.Song AND requests.client =
"jim"
WHERE tracks.artist="Rolling Stones" AND requests.Song IS NULL
Oct 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Phillip T. Murphy | last post by:
Well, after half pulling my hair out messing with this, I am thinking it is not possible. I did research and found references to "sub-queries" not possible in MySQL (I am using 4.0.18-32 with PHP...
6
by: Bob Bedford | last post by:
I've a table on mysql: code1 tinyint, code2 tinyint, notcode1 varchar(20),notcode2 varchar(20),label I do have some records: values(1,null,null,null,'text1');...
5
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: Arc | last post by:
Sorry if this has been gone over before, but I checked google groups and I couldn't find anything. Does anyone here have any idea when 4.1.x will be released into production (i.e. come out of...
57
by: Bing Wu | last post by:
Hi all, I am running a database containing large datasets: frames: 20 thousand rows, coordinates: 170 million row. The database has been implemented with: IBM DB2 v8.1
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
11
by: Sinan Osan | last post by:
Hello, I want to select the users from 'userTBL' which does not have any property in 'live_properties' table, Both table has a same column called 'username' I have 2 tables called...
0
by: yuan22m | last post by:
Hi I have encounter problem in mysql with my query regarding subquery support. The IN and NOT IN are not supported. my client's server is running the old version of mysql and he cant upgrade...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.