0

Mysql table

I need the mysql query to get the items whose nid > 910 for user_id=1 and nid > 902 for used_id <> 1.

Anybody done this? Searched but cant find it, since i am new to php and mysql.

3 Answers 3

1

Can't you use OR in your WHERE criteria:

SELECT *
FROM YourTable
WHERE (nid > 910 AND user_id = 1)
    OR (nid > 902 AND user_id <> 1)
Sign up to request clarification or add additional context in comments.

Comments

0

Try with this query:

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

Comments

0

SQL Query:

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

But if you want to call it from PHP you'll need something like this:

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

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.