Home > Software engineering >  How to get multiple rows by ID sql query?
How to get multiple rows by ID sql query?

Time:07-20

example:

ID|Name|Date|
-------------
1 |john|7/19|
2 |Bob |7/19|
3 |Burt|7/19|

Let's say I want to grab all rows where ID= 1 && 2.

How would I do so?

CodePudding user response:

Let's say I want to grab all rows where ID= 1 && 2

Don't think of it in terms of human language. Think of it in terms of boolean logic. Expressions which resolve to true/false values, which can be combined using boolean operators to resolve to true/false values.

A logical "and" won't work because there is no record for which ID=1 and ID=2. You could write that query, it would just always return no results.

What you want is any record where ID=1 or ID=2. For two of the records shown, one of those expressions will resolve to true and one to false. And true or false resolves to true.

You can combine and expand the WHERE expression all you like, as long as it still resolves to a single boolean value. Records for which that value is true will be returned, records for which it is false will not.

In that example you have two expressions, combined with an or to create a larger expression:

SELECT * FROM TableName WHERE ID=1 OR ID=2

To help illustrate, you can use explicit parentheses to group expressions:

SELECT * FROM TableName WHERE ((ID=1) OR (ID=2))

CodePudding user response:

you could use the syntax,

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
  •  Tags:  
  • sql
  • Related