简体   繁体   中英

MYSQL: can BULK select can still return array of results from inputs of same value?

mysql> select Name from Type where id in (8,8);

+------------------+
| Name             |
+------------------+
| PUBLISHER        |
+------------------+

It only returns 1 value.

Is there a way it can return 2 PUBLISHER ? since i have 2 inputs, i need that for the forEach in the js code. Thanks !

Similar to the previous answers, but perhaps a little more reflective of the behavior you're expecting, and not requiring the entire query be repeated for each value in the list, another option would be something like this:

SELECT t.Name 
FROM (
   SELECT 8 AS id
   UNION ALL SELECT 8
) AS p
INNER JOIN `Type` AS t ON p.id = t.id
;

One way is to break this into individual Select queries for each id value and then do a Union All . You can easily prepare the query in a loop for each id value input:

Select Name From Type Where id = 8
Union All
Select Name From Type Where id = 8

This query:

select Name from Type where id in (8,8)

Is syntaxically equivalent to:

select Name from Type where id = 8

The number of items in the IN operator does not tell anything about the number of records in the resultset. It just tells your RDBMS to return all records that match any of the value listed at the right of IN operator.

The results that you are getting do indicate that there is a single record in the table that has id = 8 .

A solution to get the output that you expected would be to use UNION ALL :

select Name from Type where id = 8
union all
select Name from Type where id = 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