具有多个条件的php mysql查询

时间:2014-09-19 03:56:36

标签: php mysql

Mysql table

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

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

3 个答案:

答案 0 :(得分:1)

您无法在OR条件中使用WHERE

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

答案 1 :(得分:0)

尝试使用此查询:

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

答案 2 :(得分:0)

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 />";
}
?>