简体   繁体   English

MYSQL:批量选择是否仍可以从相同值的输入返回结果数组?

[英]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. 它仅返回1个值。

Is there a way it can return 2 PUBLISHER ? 有没有办法可以返回2 PUBLISHER? since i have 2 inputs, i need that for the forEach in the js code. 由于我有2个输入,因此我需要js代码中的forEach。 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 . 一种方法是将其分解成针对每个id值的各个Select查询,然后执行“ Union All合并Union All You can easily prepare the query in a loop for each id value input: 您可以轻松地为每个id值输入循环编写查询:

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. IN运算符中的项目数不会告诉任何有关结果集中记录数的信息。 It just tells your RDBMS to return all records that match any of the value listed at the right of IN operator. 它只是告诉您的RDBMS返回与IN运算符右侧列出的任何值匹配的所有记录。

The results that you are getting do indicate that there is a single record in the table that has id = 8 . 您得到的结果确实表明表中有一条id = 8

A solution to get the output that you expected would be to use UNION ALL : 一种获得您期望的输出的解决方案是使用UNION ALL

select Name from Type where id = 8
union all
select Name from Type where id = 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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