[英]How to select multiple rows from the same table only using one query?
I have one table from MySQL and I want to select some rows from this table without having to use lots of queries to find single rows. 我有一个来自MySQL的表,我想从该表中选择一些行,而不必使用大量查询来查找单个行。
My code currently is something like that : 我的代码目前是这样的:
$query = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo = '101' ";
$query2 = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo = '102' ";
$query3= "SELECT def_conteudo FROM conteudo WHERE nro_conteudo = '103' ";
$query4 = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo = '104' ";
And I don't like it, because for me it seems useless this code but I don't know a better way to find a solution to this, as I am new to PHP. 而且我不喜欢它,因为对我来说这段代码似乎没用,但是我不知道找到解决方案的更好方法,因为我是PHP的新手。
I want something like that if possible : 我想要这样的事情:
$query = "SELECT * FROM conteudo";
And while selecting all the table, I could choose what value I would display, without having multiple queries. 在选择所有表时,我可以选择要显示的值,而无需进行多次查询。 How can I make that work ? 我该如何工作?
A couple of ways: 几种方法:
Using between: 使用之间:
SELECT def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 101 AND 104;
This fetches every row with an ID between those two number. 这将获取在这两个数字之间具有ID的每一行。 If I remember correctly, the lower end is inclusive and the higher end is exclusive. 如果我没记错的话,低端是包含性的,高端是排他性的。
Alternatively, if they are not consecutive: 或者,如果它们不是连续的:
SELECT def_conteudo FROM conteudo WHERE nro_conteudo IN (101, 102, 103, 104);
This will fetch the ID's in the list. 这将获取列表中的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.