[英]php - MySql select from table with multiple conditions
I need to select all from database when "main=1" once, then select all from database when "main=0" and use them within the same request . 我需要在“ main = 1”时从数据库中全部选择一次,然后在“ main = 0”时从数据库中全部选择并在同一请求中使用它们。 Can I do this in one request? 我可以在一个请求中做到这一点吗? this is the request 这是要求
1. 1。
$result = $apt->query("SELECT * FROM cat where main='0'");
while($row=$apt->dbarray($result)){
@extract($row);
2. 2。
$result = $apt->query("SELECT * FROM cat where main='1'");
while($row=$apt->dbarray($result)){
@extract($row);
I kind of need to combine them in one request because this will make a conflict when do 2 request. 我有点需要将它们合并到一个请求中,因为这样做2个请求时会产生冲突。 Thanks 谢谢
You have a couple of options 您有几种选择
SELECT * from cat where main = '0' or main ='1';
or 要么
SELECT * from cat where main in ('0','1');
If what you meant by a conflict is in reference to maintaining concurrency between the two queries, this way and wrapping the entire operation in a transaction are the only ways to ensure concurrency. 如果冲突的含义是指维护两个查询之间的并发性,则将这种方式并将整个操作包装在事务中是确保并发性的唯一方法。
Here is an example of how you can use the queries above to emulate the code you provided but doing it in a single query. 这是一个示例,说明如何使用上面的查询来模拟您提供的代码,但可以在单个查询中完成。
$result = $apt->query("SELECT * FROM cat where main='0' or main = '1' order by main");
while($row=$apt->dbarray($result)){
@extract($row);
switch ($main){
case 0:
// do stuff for when main = 0
break;
case 1:
// do stuff for when main = 1
break;
}
}
1) Create 1 array and insert the result inside that array. 1)创建1个数组并将结果插入该数组中。
$array = array();
$result = $apt->query("SELECT * cat where main='0'");
while($row=$apt->dbarray($result)){
$array['main0'][] = $row[];
}
$result = $apt->query("SELECT * cat where main='1'");
while($row=$apt->dbarray($result)){
$array['main1'][] = $row[];
}
echo json_encode($array);
2) OR you can use 1 query with IN
2)或者您可以对IN
使用1个查询
$result = $apt->query("SELECT * cat where main IN ('0','1')");
while($row=$apt->dbarray($result)){
$array['main0'][] = $row[];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.