使用 MySQL 从数据库表中选择大量行 [英] Selecting large amount of rows from database table with MySQL

查看:28
本文介绍了使用 MySQL 从数据库表中选择大量行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有如下属性的表:

I have a table with attributes like this:

likes(id, message, ip, time)

id 是主键并且是自动递增的.我想要做的是根据我提供的 ID 从该表中选择 30 行.

id is the primary key and is auto-incrementing. Whet I want to do is select 30 rows from this table based on an id that I provide.

我可以构建一个包含我想从表中获取的所有 ID 的随机数组,现在我只需要这样做.我通常的 SQL 查询是:

I can build a random array of all IDs I want to grab from the table, now I just need to go about doing that. My usual SQL query would be:

SELECT message
FROM `likes`
WHERE id=$id
LIMIT 1

但我需要选择 30 行.有两种方法可以做到这一点,我可以使用包含 30 个查询的 FOR 循环,或者将它们全部合并为一个查询并同时获取它们.显然我想做后者.

But I need to select 30 rows. There are two ways I can do this, I can either use a FOR loop with 30 queries, or combine it all into one query and grab them all at the same time. Obviously I would like to do the latter.

任何人都可以帮助我使用 SQL 查询一次从我的数据库表中获取多行吗?也许是我如何将这些解析为数组以便于检索的示例?

Can anyone help me out on the SQL query to use to grab more than one row from my database table at once? And perhaps an example of how I would parse these into an array for easy retrieval?

非常感谢,谢谢.

推荐答案

您可以使用 IN 运算符,所以它会是这样的:

You could use the IN operator, so it would be something like this:

SELECT message FROM likes WHERE id IN (1, 2, 3, 4, 5, ...)

这篇关于使用 MySQL 从数据库表中选择大量行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆