简体   繁体   中英

In MySQL, how do I select only rows with certain values for a given column?

Let's say I have this table:

|user_id | first_name | last_name |
----------------------------------|
| 0      |  joe       |  smith    |
| 1      |  bob       |  jones    |
| 2      |  joe       |  black    |
| 3      |  mary      |  jeane    |
| 4      |  peter     |  parker   |

How would I go about selecting the contents of the rows which correspond to the user_id's 1 , 2 and 3 ? I want to use these rows later to display their contents in a list to the user.

Try this:

select user_id,first_name,last_name 
from your_table_name 
where user_id in (1,2,3);

Replace your_table_name with the actual table name.

Place the comma separated column names that you want as output right after select keyword.
in clause is used to select between multiple values.
So in your case, query would look some how like this:

select user_id, first_name, last_name
from table_name
where user_id in (1,2,3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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