简体   繁体   English

如何仅使用一个查询从同一表中选择多行?

[英]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.

相关问题 PHP / SQL:仅使用一个查询,如果两个表中都有数据,则从两个表中选择行;否则,仅从一个表中进行SELECT - PHP/SQL: Using only one query, SELECT rows from two tables if data is in both tables, or just SELECT from one table if not MySQL - Select 一次查询中来自不同表的多行 - MySQL - Select multiple rows from different table in one query 如何仅将同一表中的多行和 select 连接到没有 NULL 值的列 + 为该列添加别名 - How to join multiple rows from same table and select only the column with not NULL values + add an alias to the column Laravel:如何从只有1个查询的同一个id的多个表中删除行? - Laravel: How to delete rows from multiple table with same id with only 1 query? 如果有多条记录,如何从MySQL表中选择一条记录 - How to select the data from MySQL table only one record if there are same multiple records mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table 从同一表中选择多行 - Select Multiple Rows From Same Table 如何从一个查询一个表中选择多个键值? - how to select multiple key value from one table with one query? 选择查询以在同一html表的不同行中显示来自多个表的数据 - select query to display data from multiple table in different rows with in same html table 如何从一个表中选择具有同一表中其他行的特定ID的行 - how to select rows from one table with specific id of other rows in the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM