php how to add two where in single query - php

i have following sql query, everything works fine but when i put "and posted_date<>$datetime" its not retrieving data as per given command.
$datetime="0000-00-00";
$data = mysql_query("SELECT * FROM product_table where category_id=$cat1 or
pid=$par or gpid=$gpar and posted_date<>$datetime
ORDER BY autoid desc limit $no2,$cacount")
or die(mysql_error());
please check is that line is ok maybe i am doing mistake somewhere where category_id=$cat1 or pid=$par or gpid=$gpar and posted_date<>$datetime
maybe i need two where one for or and another for and...
Thanks

Try to group your condition and use DATE()
SELECT *
FROM product_table
where (category_id=$cat1 or
pid=$par or gpid=$gpar) AND DATE(posted_date) <> DATE($datetime)
ORDER BY autoid desc
LIMIT $no2, $cacount

Related

How to select the top third row in mysql

Guys am trying to select the top/recently third row, i tried this one but it doesn't work, where do i make mistake ?
<?php
$sql = "SELECT * FROM songs ORDER BY id ASC LIMIT 1,2;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['artist'];
}
}
?>
Use OFFSET:
SELECT * FROM songs ORDER BY id ASC LIMIT 1 OFFSET 2
The shorthand (which you are using) is reversed, so OFFSET is first then LIMIT:
SELECT * FROM songs ORDER BY id ASC LIMIT 2,1;
Use OFFSET
Here the limit 1 It simply means and you need one record
and the offset means skip the first 2
SELECT * FROM songs ORDER BY id ASC LIMIT 1 OFFSET 2
The parameters you use after limit should be reversed.
The first parameter is offset, and the second parameter is number of record you want.
SELECT * FROM songs ORDER BY id ASC LIMIT 2,1
This is just my opinion--
Sorting like this should always be done in client software.
Extract the data - remove the ORDER BY for your SQL...
Sort it in your client, and select and return the third line to the caller.
You will get better scalability and maintainability than driving all of this through an SQL query.
This is my go-to approach when solving these types of problems through custom software and it has been proven out over time.
Think about this:
Select ID from songs
get the id's into your code, and sort them there. Then chose the third one in the list. Then:
select title, author, artist, ... from songs where ID = VALUE FROM ID ABOVE
Yes, you are hitting the database twice, but these are two very efficient queries and that will perform better as your database scales, than the fancy order by you propose.

Where do I put DESC in a mysql query? (PHP)

This is my query:
$query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage";
I'm trying to sort the results by the latest (latest on top), so I tried to add DESC.
But where ever I put it in the query it gives me an error, the only way it doesn't give an error is like this:
$query = "SELECT * FROM movies ORDER BY 'timestamp' DESC LIMIT $startAt, $perPage";
But even though it doesn't give an error, it still doesn't work.
This probably seems like a silly thing to ask but it's really driving me crazy
Timestamp is the datatype in MySQL. So, always try to avoid such column name in your table. Even, if you have a column named as timestamp, you must have to use backtick. So, use below query:
$query = "SELECT * FROM table ORDER BY `timestamp` LIMIT $startAt, $perPage";
Try backtick on timestamp :
$query = "SELECT * FROM movies ORDER BY `timestamp` DESC LIMIT $startAt, $perPage";
Using backticks permits you to use alternative characters
I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.
Do not use quote in field name. Try this:
$query = "SELECT * FROM movies ORDER BY timestamp DESC LIMIT $startAt, $perPage";

Did I mess up my where clause? Getting unexpected results

$active_sth = $dbh->prepare("SELECT * FROM user_campaign
WHERE status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
AND uid=:uid
ORDER BY status ASC");
$active_sth->bindParam(':uid', $_SESSION['uid']['id']);
$active_sth->execute();
I am positive $_SESSION['uid']['id'] = 7
but it will also pull results of id 10 or any other number.
Is my AND/OR clause written wrong?
Yes, query is wrong
SELECT * FROM user_campaign
WHERE (
status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
)
AND uid=:uid
ORDER BY status ASC
You have to group all ORs to make sure that row got one of this values, and separately check if it have given uid.
The proper way to write that is:
SELECT * FROM user_campaign
WHERE status IN ('blasting', 'ready', 'followup_hold', 'initial_hold')
AND uid =: uid
ORDER BY status ASC
You should use IN instead of that huge amount of ORs :)

there is something wrong with sql query

following sql query was working fine, i don't whats wrong i did its stopped fetching records.
$data = mysql_query("SELECT * FROM product_table where pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1 ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
when i remove or clause its works fine for example
$data = mysql_query("SELECT * FROM product_table ORDER BY autoid desc limit
$no2,20")
or die(mysql_error());
please have a look and let me know where i am doing mistake....
regards,
It seems,your query is OK, but when you use WHERE clause, you limit the results , so perhaps there is no recored for display, especially when you use LIMIT for starting offset and number of results.
Your query is ok but there is no record to satisfy your where part. go to your database and create some new rows with your criteria.
Try:
$data = mysql_query("SELECT * FROM product_table where (pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1) ORDER BY autoid desc limit $no2,20")
or die(mysql_error());

Joining 2 mysql queries

I have two tables which I need to select all rows from where the userid is $userid then sort them. I tried to use join but I couldn't even really get started on how to get it right. Can anybody point me in the right direction as to how to make these into one query?
$result1 = mysql_query("SELECT * FROM paypal_sub_info
WHERE userid='$THEuserid' ORDER BY cur_time DESC");
$result2 = mysql_query("SELECT * FROM paypal_pay_info
WHERE userid='$THEuserid' ORDER BY cur_time DESC");
while($row = mysql_fetch_array($result1)){
echo $row['txn_type'];
}
Solution:
SELECT *
FROM paypal_sub_info sub,paypal_pay_info pay
WHERE pay.userid = '$THEuserid'
AND sub.userid = '$THEuserid'
ORDER BY pay.cur_time DESC,sub.cur_time DESC
Try this:
SELECT * FROM paypal_sub_info sub, paypal_pay_info pay
WHERE pay.userid='$THEuserid' AND sub.userid='$THEuserid'
ORDER BY pay.cur_time DESC, sub.cur_time DESC
If you just want 'txn_type', you could make it a SELECT pay.txn_type AS txn_type
Use:
SELECT psi,*, ppi.*
FROM PAYPAL_SUB_INFO psi
JOIN PAYPAL_PAY_INFO ppi ON ppi.userid = psi.userid
WHERE psi.userid = $THEuserid
ORDER BY psi.cur_time DESC, ppi.cur_time DESC
I believe you want:
SELECT field1, field2, ... FROM paypal_sub_info WHERE userid='$THEuserid'
UNION
SELECT field1, field2, ... FROM paypal_pay_info WHERE userid='$THEuserid'
ORDER BY cur_time DESC
So first off consider using mysqli for for any serious project. OMG Ponies answer is how I would suggest doing it, thought you shouldn't have to specify the alias.wildcard fields separately in the select clause. It's also a best practice to actually specify the fields you are trying to fetch rather than *, though we all use * a lot when we're lazy.
Will A's answer makes me smile because it's technically what you asked for, though not what I expect you wanted.
Do you have a more detailed description of what data you're trying to extract, or was this just an example because you are having trouble figuring out joins?
-J

Categories