0

I have a mysql table as below,

+---+-------+-----------+   
|Sl | Name  | Status    |
+---+-------+-----------+   
| 1 | Name1 | Active    |
| 2 | Name2 | Inactive  |  
| 3 | Name3 | Pending   |    
| 4 | Name4 | Dont Know |   
| 5 | Name5 | Active    |
+---+-------+-----------+   

I have a sql query $query="select * from table "; which fetch array and give me all records. But I want to display only specific rows with Status= Active or Inactive.

4 Answers 4

1

Add a WHERE clause to your SQL so it becomes,

SELECT * FROM table WHERE Status='Active'

So in your PHP code it would be,

$query = "SELECT * FROM table WHERE Status='Active'";

Then replace "Active" with "Inactive" or "Pending", etc. depending on what you which rows you want to get.

Update 1: If you want rows that are both "Active" and "Inactive", you can add multiple expressions in the WHERE clause and combine them with "OR". For example,

SELECT * FROM table WHERE Status='Active' OR Status='Inactive'

Update 2: You can add your !='Trashed' condition if you want, but it will have no effect on the outcome since you're only returning rows that are explicitly "Active" or "Inactive" anyway. But as this is just an example, add this to the end of the query,

AND Status!='Trashed'
Sign up to request clarification or add additional context in comments.

2 Comments

thanks.. but how if i want to add another parameter not equal to, suppose if i want to add a a parameter Status !='Trashed' , how can i add this to the query??
In your example you wouldn't need to, since it will only return rows that are "Active" or "Inactive". Ones which are "Trashed" would never appear in your results so you wouldn't need to include that condition. But you can use other operators such as "AND", "NOT", etc. in the WHERE clause however you need.
1

use WHERE clause with OR operator

SELECT * FROM table WHERE Status='Active' OR  Status='Inactive'

its simple add to the query

AND Status !='Trashed' 

2 Comments

thanks.. but how if i want to add another parameter not equal to, suppose if i want to add a a parameter Status !='Trashed' , how can i add this to the query??
look in the link i post about all the logical operators . dev.mysql.com/doc/refman/5.0/en/logical-operators.html
1

SELECT * FROM table WHERE Status IN ("Active", "Inactive");

4 Comments

thanks.. but how if i want to add another parameter not equal to, suppose if i want to add a a parameter Status !='Trashed' , how can i add this to the query??
SELECT * FROM table WHERE Status IN ("Active", "Inactive") and status not in ("not", "your") and status <> "Trashed";
The imagination is the limit :-)
... AND type like "%pie" AND (flavor LIKE "%berry" OR flavor = "Lemon") AND flavor <> "apple"
1

You need to alter your SQL

SELECT * FROM table WHERE Status = 'Active' OR Status = 'Inactive'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.