Use Three Conditions In WHERE Clause Of Mysql Query?

May 30, 2011

Here is my current MySQL syntax (but this doesn't work):

SELECT * FROM posts WHERE userID = $userID AND postBy = $postBy AND postType = 'unread'

Here is the picture of my MySQL table (a picture can show more than words!) I want to know if there is any other way of doing it or if I have to forget this idea?

View 2 Replies


Multiple Conditions In Where Clause

I need multiple conditions in the where clause, I did this and it is not working.

$result5 = mysql_query("SELECT * FROM test WHERE (date BETWEEN '$sqlfrom' AND '$sqltoprevious') AND room_id = '$room_id' AND hotel_id = '$hotel_id'") or die (mysql_error());
while ($row5=mysql_fetch_array ($result5))
$dates = date('d M Y', strtotime($row5['date']));
$rate = $row5['rate'];

So, what I want is the dates and rates from the table test where the dates are between $sqlfrom and $sqltoprevious and where the room id is $room_id and hotel id is $hotel_id.

View 2 Replies

Mysql Query Multiple Conditions.

Hi i want to do a mysql select with more than 1 conditions.

I have this:

select * from phpbb_posts where post_time = 1064078039;

but i want to be able to select multiple post_time's. I'm guessing it was something like this:

select * from phpbb_posts where post_time = (1064078039, 1034078039, 10640744039, 10643378039, 1064076039);

but thats obviously wrong. Anyone can help here?

View 1 Replies

MySQL Query With Multiple WHERE Conditions

however I've not seen any result that would come close to my case. I have a query with 3(THREE) WHERE conditions to be met as follows:

$sql = mysql_query("SELECT type_name, COUNT(type_name) FROM tb_tracker WHERE session_id = '".$id."' AND type_name = 'deposit'AND expiry > '".$time."' GROUP BY type_name");

I know the query above is flawed because the second AND will not get executed however I'd like to use all of the conditions above OR didn't do the trick for me

View 2 Replies

Simple MYSQL Query - 2 WHERE Conditions Syntax

I am trying to formulate a mysql select statement where it selects all the fields below I am having trouble however with WHERE indMatch.player2ID = '$username' I want to add to this WHERE indMatch.p2ConfirmResult is equal to 0 aswell but I do not know the syntax. Code:

View 4 Replies

Switch MySQL Query Conditions, After All Results Listed?

Let's assume I have a table with the fields "fruit" and "color". What I am looking for is a loop where in the first round all fruits with red color will be selected and in the second round all as green fruits.

//first round
$fruit_color = "red"
//second round
$fruit_color = "green"

View 4 Replies

Write A MySQL Query With A Variable In The WHERE Clause?

how to write a MySQL query with a PHP variable in the WHERE clause. I've tried {} {'xx'} and () and it still doesn't work.

Here is the code

include ("include/connect.php");
include ("include/session.php");


So I'm trying to show the email address for a record that matches the username of the user logged in.

View 5 Replies

MySQL Query - INSERT - Unknown Column 'test' In 'where Clause'

I have a registration script on my website, and it was working fine, I added a second MySQL INSERT query to it, for another table so I can use JOIN's later on with the second table, but I keep getting the same error:

Unknown column 'test' in 'where clause' can someone see whats wrong with this? Code:

View 6 Replies

Mysql If Conditions? Loops Conditions?

Are if statements and looping possible in a sql command line?

Well, actually I want to Select only 2 unique "ids" from a table to be shown per page....but this seems impossible to do using mysql. But I want to verify.

If the ids are listed like this:


Can I use a mysql query to only show 1 and 2 on one page, 3 and 4 on another page, 5 and 6 on another page, and so on?

View 1 Replies

Mysql OR Clause Respective To AND Clause?

I have a form for filtering/sorting database records.

I'm filtering posts by author, category, and tags. I'm using AND clause for author and category and OR clause for tags (multiple tags can be entered)

A query looks like this

FROM (`posts`)
WHERE `author` = 'dan'[code]....

The query above returns posts that contain any tag searched regardless of the author or category.I want to return posts containing tags ebook, ipad, apple only within the author name dan and category technology.

View 2 Replies

UPDATE Clause In MYSQL Update Multiple Records In One Query?

well i have this messages table with sample values like these:

msg_id recipient_id read locked new
0 1 N Y Y
2 1 Y N N

ok, so lets just say this is a messaging table, and i want to reset all messages addressed to recipient with id=1 i was wondering why UPDATE `messages` SET `new`='Y',`read`='N',`locked`='N' where `recipient_id`=1;

doesn't work, MYSQL always returns 0 affected rows.. to robert gamble: yes, im sure the values were changed, since my purpose for this update query is to reset the data i was using for the testing phases :D

View 3 Replies

REGEX: Can't Seem To Replace LIMIT Clause In Query Using Preg_replace ($pattern, $replacement, $query)

Want to replace the limit clause in a query, but can't get it right.
What's wrong with this:

$pattern = "(.*)limit (.*)";
$replacement = '$1'
$replacement .= "LIMIT $limit";
$replacement .= '$2'
$query = preg_replace ($pattern, $replacement, $query);

View 1 Replies

MySQL Ignores Indexes If Query Contains Conditions "greater Than" Or "smaller Than"?

why MySQL is not using indexes in queries with conditions "greater than" or "is not empty":

PHP Code:

select from table where col1='$col1' and col2 !='';

PHP Code:

select from table where col1='$col1' and col2 > '0';

Indexes are used only if query contains condition "equal to":

PHP Code:

select from table where col1='$col1' and col2 ='$col2';

Don't know how can I use indexes if I need to select columns where value is greater than something or value is not empty

View 2 Replies

Search Form Returning Error "Invalid Query: Column 'name' In Where Clause Is Ambiguous SQL Query"

This is my code and the error is underneath.

$search = $_POST["search"];
$db = "`char`";
$connection = mysql_connect("localhost","wowbasec","PASSWORD REMOVED");
mysql_select_db("wowbasec_class", $connection);
$query = "select * from characters, location WHERE name='$search'";

Invalid query: Column 'name' in where clause is ambiguous SQL Query: select * from characters, location WHERE name='Harry'

View 8 Replies

Trying To Put A Sub - Clause Into A Query

I have 2 tables "properties" and "images" the images table contains all the images for each property, linked via a 'prop_id' at the moment I'm selecting all the properties and just 1 image for each property using this query :

$sql = "SELECT DISTINCT(t1.prop_id) AS pid, t2.image_thumb,t1.prop_name,t3.loc_type_name
FROM properties AS t1
LEFT JOIN images AS t2 ON t2.prop_id=t1.prop_id
LEFT JOIN location_types AS t3 ON t3.loc_type_id=t1.loc_type_id
WHERE t1.prop_online='1'
ORDER BY t1.prop_name ASC";

this works fine but my problem is that the images are ordered using a field called "image_order" and I'd like the first image in the order (with an "image_order" value of 1) to be selected by this query but what's happening is that the query above is selecting the image with the lowest "image_id" (the first image uploaded) and NOT the first image in the ordering So ... what I'd like to do is to include a bit in the query that says "select the lowest image_order value" kind of like this pseudo-code :


View 1 Replies

Speed Up Query With No WHERE Clause?

I have about 1 million rows so its going pretty slow. Here's the query:

$sql = "SELECT `plays`,`year`,`month` FROM `game`";

I've looked up indexes but it only makes sense to me when there's a 'where' clause.

View 5 Replies

Fetch Rows From Wpr_subscribers Provided That The Conditions In The Query?

I have the following query to fetch rows from wpr_subscribers provided that the conditions in the query.

SELECT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions subscriptions WHERE AND AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index

The query returns two of every row. How do I solve this issue?

View 2 Replies

Pull Data From A Second Sql Query Using A Where Clause

I am trying to pull data from a second sql query using a where clause = two fields from the first query. I am trying to gather from the second query, any data where we have a match from either of the two in the where clause.

sql=my select 1,2 from t1

while $row {


sql=select 1,2 from t2 WHERE 1 = '$one' or 2 = '$two' group by 1,2

while $row2 {

//trying to output all rows that either 1 or 2 from t2 match 1 or 2 from t1. so all grouped results from 1 display all unique results in 2all grouped results from 2 dispaly all unique results in 1 where either 1 from t1 = 1 from t2 or 2 from t1= 2 from t2.

View 5 Replies

Conditionally Add A Clause To Query In Script?

In my search form I have, value male, value female, and value both. If you select both, I do not wish to have at WHERE the "sex = '$sex'" Should i do it like this:

if(empty($sex)){ // value empty if you chose "both"
$query = "SELECT firstname, lastname, id, user_name, sex, last_access, bostadsort FROM users WHERE (firstname LIKE '%$firstname%' OR lastname LIKE '%$lastname%')";
$query = "SELECT firstname, lastname, id, user_name, sex, last_access, bostadsort FROM users WHERE (firstname LIKE '%$firstname%' OR lastname LIKE '%$lastname%') AND sex = '$sex'";

View 3 Replies

Doctrine Sql Query, Where Clause Not Taken Into Account?

I suppose my problem is simple but i can't get it fixed...Here is my query:

$this->invites = Doctrine_Query::create()
->from('Utilisateur u')

View 1 Replies

Doctrine - Clause Included Only When Necessary In A Query?

I want to use a single query to retrieve: items of any categories (no filter applied); only items of a single category (limited to a particular category); For that purpose I should be able to write a Doctrine query which would include a where clause only when certain condition is met (eg. part of URL existing), otherwise, where clause is not included in the query. Of course, i tried with using the If statement, but since doctrine query is chained, the error is thrown.

So i guess the solution might be some (to me unknown) way of writing doctrine queries in an unchained form (by not having each row started with "->" and also having each row of a query ending with semicolon ";")

That way the usage of IF statement would be possible i guess. Or, maybe there's already some extremely simple solution to this matter?

View 2 Replies

Making A SQL Query With An Array In WHERE Clause

i have a problem making a SQL Query with an array in my WHERE clause.

For example:
My Array:
$myarray[1] = "hi";
$myarray[2] = "there";
$myarray[3] = "everybody";
My MySQL Statement:


But if i had thousands of entries in my array, the SQL Statement would be too big and slow, right?

View 4 Replies

Use Prepared Statement In Query With 'IN(.....)' Clause?

Is there a (tricky/non-standard) way to do this?Would something like

$stmt = $db->prepare( 'SELECT title FROM episode WHERE id IN (?, ?, ?, ?);

work? So if I wanted to search for a variable amount of ids, could I do

$ids = array(1,2,3,4,5,6,7);
$idSection = implode(array_pad(array(), count($ids), '?')) //To give ?,?,?,?,?,?,?
$stmt = $db->prepare( 'SEL

View 1 Replies

Query Based On 2 Conditions - Select If The Doc_type Is Either S OR F But This Doesn't Work

I want to select if the doc_type is either s OR f but this doesn't work:

Code: [Select]$query2 = "SELECT * FROM members WHERE doc_type='s' && doc_type= 'f' ORDER BY username";

Also If I want to do two sorts can I do this: Code: [Select]$query2 = "SELECT * FROM members WHERE doc_type='s' && doc_type= 'f' ORDER BY doc_type, username";

View 4 Replies

Select Query With WHERE Clause Doesn't Work Any Longer

I have a table in MYSQL that I'm trying to set up a search query on (so it can be searched for records by an author's name). I did it several weeks ago and then it worked, but now when I try to run the search it returns no records although I know they are there. To my knowledge nothing has changed in my PHP script. I checked everyhting repeatedly but must be blind to the error. It's driving my crazy since I know it worked fine at one time.

If anyone has the energy to take a look and offer their suggestions, it would be greatly appreciated. Here is my script. PS. I can add a record and view all the records through other scripts with no problem.

<?php # Script 11.6 - md_seaauthor.php
// This page searches titles by author.

// Set the page title and include the HTML header.
$page_title = 'Search Mother Daughter Bookclub by Author'
include_once ('header4.html');

require_once ('Connections/mysql_connect.php');
// Connect to the database.

$query = "SELECT * FROM mdbookclub WHERE author ='$author'";
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result)) {
$md = $row['md_id'];
$title = stripslashes($row['title']);
$author = stripslashes($row['author']);
$f_author = stripslashes($row['f_author']);
$pub_year = stripslashes($row['pub_year']);
$call_number = ($row['call_number']);
$display_block .= "<p><strong>Author:</strong> $author <strong>Title:</strong> $title<br />
<strong>Publication year:</strong> $pub_year <br />
<strong>Call number:</strong> $call_number </p>";

View 1 Replies

My Code POSTS The ID From Each Menu Then Puts That ID Into The WHERE Clause Of An SQL Query.

I've got two drop down menus that I'm using to filter some data. It works when the user selects an option from both menus but always return ZERO records when only one of out the two menus have been set. My code POSTS the ID from each menu then puts that ID into the WHERE clause of an SQL query. Code:

View 2 Replies

Copyrights 2005-15, All rights reserved