繁体   English   中英

PHP的MySQL查询多个条件

[英]php mysql query with multiple conditions

MySQL表

我需要mysql查询以获取其nid> 910 for user_id = 1和nid> 902 for used_id <> 1的项目。

有人这样做吗? 搜索,但找不到它,因为我是php和mysql的新手。

您不能在WHERE条件中使用OR

SELECT *
FROM YourTable
WHERE (nid > 910 AND user_id = 1)
    OR (nid > 902 AND user_id <> 1)

尝试使用以下查询:

SELECT * FROM items WHERE (nid>910 AND user_id=1) OR (nid>902 and user_id!=1)

SQL查询:

SELECT * FROM table_name WHERE (nid>910 AND user_id=1) OR (nid>902 and user_id!=1);

但是,如果要从PHP调用它,则需要这样的东西:

<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost"; 
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("exampledb",$dbhandle)
  or die("Could not select exampledb");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_name WHERE (nid>910 AND user_id=1) OR (nid>902 and user_id!=1);");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
   echo "ID:".$row{'user_id'}." Nid:".$row{'nid'}."<br />";
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM