使用多个参数获取Mysql - php查询数组结果

时间:2022-09-25 16:17:06

I have a mysql table as below,

下面是mysql表,

+---+-------+-----------+   
|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.

我有一个sql查询$query="select * from table ";获取数组并给我所有的记录。但我只想显示状态=活动或非活动的特定行。

4 个解决方案

#1


1  

Add a WHERE clause to your SQL so it becomes,

在SQL中添加WHERE子句,

SELECT * FROM table WHERE Status='Active'

So in your PHP code it would be,

在PHP代码中,

$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.

然后将“Active”替换为“Inactive”或“Pending”等等,这取决于您想要获取哪些行。

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,

更新1:如果想要同时属于“活动”和“非活动”的行,可以在WHERE子句中添加多个表达式,并将它们与“或”组合。例如,

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,

更新2:您可以添加您的!='Trashed'条件,但它不会对结果产生影响,因为您只返回显式“活动”或“不活动”的行。但这只是一个例子,把这个加到查询的末尾,

AND Status!='Trashed'

#2


1  

use WHERE clause with OR operator

使用WHERE子句与或操作符

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

its simple add to the query

它可以简单地添加到查询中

AND Status !='Trashed' 

#3


1  

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

从状态为(“Active”、“Inactive”)的表中选择*;

#4


1  

You need to alter your SQL

您需要修改SQL

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

#1


1  

Add a WHERE clause to your SQL so it becomes,

在SQL中添加WHERE子句,

SELECT * FROM table WHERE Status='Active'

So in your PHP code it would be,

在PHP代码中,

$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.

然后将“Active”替换为“Inactive”或“Pending”等等,这取决于您想要获取哪些行。

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,

更新1:如果想要同时属于“活动”和“非活动”的行,可以在WHERE子句中添加多个表达式,并将它们与“或”组合。例如,

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,

更新2:您可以添加您的!='Trashed'条件,但它不会对结果产生影响,因为您只返回显式“活动”或“不活动”的行。但这只是一个例子,把这个加到查询的末尾,

AND Status!='Trashed'

#2


1  

use WHERE clause with OR operator

使用WHERE子句与或操作符

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

its simple add to the query

它可以简单地添加到查询中

AND Status !='Trashed' 

#3


1  

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

从状态为(“Active”、“Inactive”)的表中选择*;

#4


1  

You need to alter your SQL

您需要修改SQL

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