简体   繁体   中英

Subquery on MySQL

I know that may be a repeated question, but I cannot make my mind about it and cannot find the right example.

I'm trying to make a subquery selecting elements with an id x, y or z from a table and, from that selection, the elements with an area xx, yy or zz.

That'd be the query:

SELECT * FROM establecimientos 
INNER JOIN subtipos ON (establecimientos.subtipos_idsubtipo = subtipos.idsubtipo) 
WHERE subtipos_idsubtipo = 11  OR  subtipos_idsubtipo = 12  OR  subtipos_idsubtipo = 13  OR  subtipos_idsubtipo = 14 
AND zonas_idzona in 
(SELECT * FROM establecimientos 
WHERE zonas_idzona = 2  OR zonas_idzona = 3  OR zonas_idzona = 4  OR zonas_idzona = 5  OR zonas_idzona = 6  OR zonas_idzona = 7  OR zonas_idzona = 8)
LIMIT 0, 90

But I get the error only a single result allowed for a SELECT that is part of an expression

I'm in a big headache! Thanks!

It's

... zonas IN (SELECT * FROM ...)
                     ^---here

The subquery is returning MULTIPLE fields for every result row, which means the DB has absolutely NO idea which of those fields it should use to compare with zonas .

Change it to

... zonas IN (SELECT specific_field FROM ...)
                      ^^^^^^^^^^^^^^---changed

instead.

You need to update the sub-query to only pull the field you will be using.

Change this:

SELECT * FROM establecimientos 
WHERE zonas_idzona = 2  OR zonas_idzona = 3  OR zonas_idzona = 4  OR zonas_idzona = 5  OR zonas_idzona = 6  OR zonas_idzona = 7  OR zonas_idzona = 8

to:

SELECT <Field Name> FROM establecimientos 
WHERE zonas_idzona = 2  OR zonas_idzona = 3  OR zonas_idzona = 4  OR zonas_idzona = 5  OR zonas_idzona = 6  OR zonas_idzona = 7  OR zonas_idzona = 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM