Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this query and it's shows this message-
"at most one record can be returned by this subquery"
i work with c# and access, can someone please help me?

What I have tried:

command.CommandText = "select id_women as תעודת_זהות_אישה,fname as שם_פרטי,lname as שם_משפחה,picture as תמונה,meeting as במפגש from women2 where (age=(select age_min from men_expectations2) and height=(select height_min from men_expectations2) and body_structure=(select body_structure from men_expectations2) and health=(select health from men_expectations2) and status=(select status from men_expectations2)) and (origin=(select origin from men_expectations2) or hashkafa=(select hashkafa from men_expectations2) or living_area=(select living_area from men_expectations2) or smoking=(select smoking from men_expectations2) or tfila=(select tfila from men_expectations2)) and Change_residence=(select Change_residence from men_expectations2) and headdress=(select headdress from men_expectations2 WHERE id_men = '" + textBox1.Text + "');";
Posted
Updated 1-May-19 23:46pm

When you use SELECT as part of a condition, the subclause can only return a single item because that is how comparison operators like "=" work: if you compare one item with two others, the result is not defined, just as it isn't in the real world. Is this orange equal to this (one apple, two bananas, one orange) or not? You don' know, I don't know - simply because there is no "real" answer.

Probably, you need to do a JOIN instead, but since we have no idea of the structure of your tables and the relationships between them, we can't say for sure, or recommend any code you could try. So think about the data in your tables, and exactly what you are trying to retrieve before you start trying to code a complex query! Or give us sample input and output and explain why it's like that ...

But you have a much more serious problem!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Your subqueries should only return one record, in this context. You need some sort of where statement that joins to one record.

This is a terribly long query. This many subqueries will perform badly, once it's working. I'd look at using CTEs and joins instead
 
Share this answer
 
Comments
ortal1 2-May-19 5:37am    
can you help me to write a better query?
Christian Graus 2-May-19 5:43am    
I can't even read this. lose the aliases when you ask english speakers for help. I'm in admiration of your bilingual skills, but they don't help me read it. Also format it better. But I'd need to know the data format to try to write a query that would work. Try explaining to me what you think the subqueries will do
ortal1 2-May-19 6:12am    
I'm sorry for the language...
the subqueries need to display the parameters:id_women,fname,lname,picture,meeting from the table "women2" if the specific parameters that i choosed from the table "women2" is equal to the specific parameters of the table "men_expectations2" ,the search done by the id_men from the table "men_expectations2" that the user enter to the "textbox1".
The general idea of the program is to match between mens and womens.
please i really need help!
i hope i explained myself better this time...
Christian Graus 2-May-19 6:17am    
OK, so you need to join between those two tables on those parameters then? You don't need subqueries for that. Just join the men to the women and do a join on these possibilities
ortal1 2-May-19 6:28am    
can you show me how to do this? i don't know how to use in join and i'm afraid to be stuck with this for a long time..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900