Home  >  Q&A  >  body text

mysql in 查询, 里面的每个条件都指定返回条数,能否实现,怎么写?

mysql in 查询, 里面的每个条件都指定返回条数,能否实现,怎么写?

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2)

table_name 里面包含value1和value2的值有很多条,能否指定返回条数。

类似:

select * from table_name where column_name = value1 limit 5;
select * from table_name where column_name = value2 limit 5;

或有其他方法实现这个查询?

高洛峰高洛峰2717 days ago759

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 16:15:28

    It is the conversion of or/in/union all. The performance is also better than union all, and it can also meet the requirement of returning the specified number of items as you said.

    (select * from table_name where column_name = value1 limit 5)
    union all
    (select * from table_name where column_name = value2 limit 5);

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 16:15:28

    Your sql seems inconsistent with your description
    SELECT column_name(s),count(column_name(s))
    FROM table_name
    WHERE column_name(s) IN (value1,value2,...)
    group by column_name(s) )

    There is nothing logically wrong with this way of writing, but please take a look at the execution plan after writing it. If I remember well, the efficiency of mysql's in is not high. If it is a single table, I can't think of anything good. Idea

    reply
    0
  • Cancelreply