Manu
Manu

Reputation: 51

How to obtain multiple rows in one query?

I have a small table in MySQL as follows:

 pid   pName    pPrice
  1    TBall    100.00
  2    MBall    200.00
  3    TJers    250.00
  4    MJers    275.00

I want to obtain multiple row as a result in a single query:

Select * from <tablename> where pName='Tball' and 'MBall';

(This code is wrong; I want an correct solution that is similiar to this.)

The query should yield the following result:

pid  pName  pPrice
 1   TBall  100.00
 2   MBall  200.00

Upvotes: 2

Views: 44

Answers (1)

Magisch
Magisch

Reputation: 7352

The question would be:

  • Q. "What did I do to avoid getting all the rows?"

  • A. You added a constraining WHERE clause

  • Q. "How do I make it les restrictive?"

  • A. Make the constraint less restrictive.

  • Q. "Do you have some examples of how I can do that?"

  • A. Well yes. There's an expression syntax for the where clause. Among others there are the logical operators (AND, OR, NOT), you can check equality (=, IS NULL). Many dialects of SQL have additional operators (BETWEEN, LIKE) and lastly all should have the IN operator.

All operators can be combined, and sub expressions can be enclosed within parentheses like you would to distinguish 3 * (7 + 5) from (3 * 7) + 5.

Here you could simply use the OR logical operator:

SELECT * FROM <tablename> WHERE (pName = 'TBall') OR (pName = 'MBall');

Or, you can use IN:

SELECT * FROM <tablename> WHERE pName IN ('TBall','MBall');

Or, you can use even LIKE for your use case:

SELECT * FROM <tablename> WHERE pName LIKE '%Ball';

Upvotes: 4

Related Questions