Solved

SQL query error:  Operand should contain 1 column(s)

Posted on 2009-06-28
5
670 Views
Last Modified: 2012-05-07
When I run the sql query below I get an error that says "Operand should contain 1 column(s)"  It is probably something simple I'm new to trying to write two statements together.  Any thoughts?
$data = mysql_query("SELECT username, first_name, last_name, middle_name FROM users WHERE username IN (SELECT * FROM users WHERE state_id=".$session_state_id.") ORDER BY ".$sorter." ASC")

Open in new window

0
Comment
Question by:jbpeake
  • 2
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
gamebits earned 500 total points
ID: 24732005
You are trying to do a subquery in the same table than the query, not sure what you are trying to achieve, would this not be working

$data = mysql_query("SELECT * FROM users WHERE state_id = '".$session_state_id."' ORDER BY '$sorter' ASC");
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24732010
IN ( SELECT * ...)
is wrong, as SELECT * returns multiple columns.
this would work better:
$data = mysql_query("SELECT username, first_name, last_name, middle_name FROM users WHERE username IN (SELECT username  FROM users WHERE state_id=".$session_state_id.") ORDER BY ".$sorter." ASC");

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24732012
of course, gamebits's query code is more efficient, anyhow :)
0
 

Author Comment

by:jbpeake
ID: 24732339
Ughh... painfully obvious when I see what you done.  Thanks.
0
 
LVL 28

Expert Comment

by:gamebits
ID: 24732386
" painfully obvious " believe me I know the feeling, thanks for the points and the grade.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 count occurrences of each item in an array.

773 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