Jump to content

WHat is wrong with this SQL statement


hosker

Recommended Posts

Here is my SQL statement:

 

$sql2 = "SELECT username from clp_user WHERE ide != 1 AND username NOT IN (SELECT * from test_picks WHERE t_id = 3)";
	$res2 = mysql_query($sql2) or die (mysql_error());
		while($row2 = mysql_fetch_array($res2))
{
	echo $row2[username] . '<br />';
};

 

I have two tables within my DB. I want to select all the usernames from the users table that are not in the test picks table that have a t_id = 3. This statement is returning all of the usernames in my users table, why is that?

Link to comment
Share on other sites

Your subquery should only select a single column, not *.  However, you could probably do what you want better using a left join query instead.

 

I took a guess that the column in test_picks that your matching with the username column in clp_user is also named username


SELECT c.username 
FROM clp_user c
LEFT JOIN test_picks t ON t.username=c.username AND t.t_id=3
WHERE 
ide != 1 
AND t.username IS NULL

 

Link to comment
Share on other sites

I am not very familiar with joins within MySQL. However, I got the code to produce the results I needed by changing "AND username" to "AND user". However I am trying to run a 2ndquery with the first queries results and am having no luck. Here is the following code so far:

 

$sql2 = "SELECT username from clp_user WHERE ide != 1 AND user NOT IN (SELECT * from test_picks WHERE t_id = 3)";
	$res2 = mysql_query($sql2) or die (mysql_error());
		while($row2 = mysql_fetch_array($res2))
{
	//echo $row2[username] . '<br />';
	$sql = "SELECT * from test_picks WHERE t_id = 2 AND user = $row2[username]";
	$res1 = mysql_query($sql1) or die (mysql_error());
	while($row1 = mysql_fetch_array($res1))
	{
		echo $row1[user] . '<br />';
	};

};

 

I echoed out the user to make sure that I was pulling the right rows.

 

The output I am getting is: Unknown column 'user' in 'IN/ALL/ANY subquery'

 

I do have a column user in the test_picks table.

Link to comment
Share on other sites

The code that I changed gave me the right result once, but has given me errors since. Would a join statement make what I am trying to do easier? Here are the coloumns in my DB tables:

 

clp_users:

 

ide username password role status fname lname dob email secquestion secanswer createdby creationtime

 

 

test_picks:

 

id t_id tournament user player backup timestamp email

 

I am trying to pull all the users who did not make a pick for the current tournament, go back to the previous week and grab that particular users backup pick. After I do that I want to insert that into my database with particular parameters. I know how to do the insert I just need help in creating the correct set of SQL statements. Any and all help is appreciated. Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.