Query wildcard with IN - php

I have this query:
select * from orders where week_of_year = '40' and user_id = '8631' and charge_date like in ('2017%','2018%') order by id desc limit 1
I have a syntax error near charge_date, I know the error I'm guessing I can't use like with 'in', is there anyway I can do the same logic that would actually work?

I guess, you need only year from the date and you want to search for multiple possible years.
Then your solution should look like below:
select * from orders where week_of_year = '40' and user_id = '8631' and YEAR(charge_date) REGEXP '2017|2018' order by id desc limit 1
Update:
Alternative Solution with IN
select * from orders where week_of_year = '40' and user_id = '8631' and YEAR(charge_date) IN (2017, 2018) order by id desc limit 1

Related

PHP: MYSQLI Change Order By Style

Hi, i want to get data from chat table like this:
6
7
8
My Code show like this:
8
7
6
Code:
"SELECT * FROM `chat` WHERE
`chat-code` = 'vm1mxo3dpi9gzuo' AND (`user_1` = '1' OR `user_2` = '1')
ORDER BY id DESC LIMIT 3"
Because you're ordering your result in descending order. You should remove the desc in ORDER BY. You can explicitly put ASC to indicate the order as ascending.
Try to read what will happens if you use DESC or ASC statements. https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
Hope it will help you.
use this mat and you can change the between parameters with prepared statements.comment if doesn't work.
"SELECT * FROM `chat` WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND (`user_1`='1' OR `user_2` = '1')
AND `id` between 6 and 10 //starting and ending offset for query so query doesn't take long time
ORDER BY id ASC LIMIT 3" // LIMIT 3 as you mentioned and ASC order
SELECT a.*
FROM
( SELECT *
FROM chat
WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND 1 IN(user_1,user_2)
ORDER
BY id DESC
LIMIT 3
) a
ORDER
BY id;
I have the solution of your problem - use below query:
SELECT * FROM (
SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo' AND
(`user_1` = '1' OR `user_2` = '1')
ORDER BY `text` DESC LIMIT 3
) t ORDER BY `text` ASC
SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo'
AND (`user_1` = '1' OR `user_2` = '1')
ORDER BY id LIMIT 3 OFFSET 5
Try above query.
You can simply change your query to this (order by text ASC):
SELECT * FROM `chat`
WHERE `chat-code` = 'vm1mxo3dpi9gzuo' AND
(`user_1` = '1' OR `user_2` = '1')
ORDER BY text ASC LIMIT 3
If you still want to order by ID, simply change the DESC to ASC

mysql select rows with top max value

i have a table in mysql with columns: id , view , agree.
i have upload my table's image below:
i want to select 8 rows that greater than others in view column. my condition is agree = 1.
can you tell me how can i do it by mysql query or php.
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
Try this:
SELECT * from table
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
use limit and order by
Select * from mytable
where aggree=1
ORDER BY view DESC
LIMIT 8
You can do this:
SELECT *
FROM yourTableName
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
You have to use ORDER BY clause.
SELECT * FROM <TABLE_NAME> WHERE AGREE = 1 ORDER BY VIEW DESC LIMIT 8
Use ORDER BY for DESC or ASC sorting.
and For selection of 8 rows use Limit Data Selections From a MySQL Database
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
If you want to take the top 8 'view' values from the table the MySql query for that is:
SELECT * FROM tablename WHERE agree=1 ORDER BY view DESC LIMIT 8;
Note: Replace tablename with the actual name of your table

Select attribute of table based on min value from another table

My table structure is
I want to get the recent video uploaded of the user
What I want to do is:
"select video_id of a record who is having minimum of timestamp where userid='something'"
What I currently have is:
$recent_video = "$db->query("
SELECT video_id
FROM video_primary
ORDER BY timestamp DESC LIMIT 1
WHERE userid = '$userid'"
) or die($db->error);"`
while($row=mysqli_fetch_assoc($recent_video))
{
$video_id=$row['video_id'];
}
echo $video_id;
My table data is
How do I get the most recent view uploaded by the user?
Write WHERE clause after FROM part
Try this:
SELECT vp.video_id
FROM video_primary vp
WHERE userid='$userid'
ORDER BY vp.timestamp DESC
LIMIT 1;
try this
$recent_video=$db->query("select video_id from video_primary where userid='$userid'
ORDER BY timestamp DESC LIMIT 1 ") or die($db->error);
instead of
$recent_video="$db->query("select video_id from video_primary ORDER BY timestamp DESC
LIMIT 1 where userid='$userid'") or die($db->error);"
you are using where clause after order by and limit 1 which is a syntax error. see tutorial
Tutorial
There is a specific sequence of every clause in Query statement. Below is the sequence:
Select
Where
Order by
Limit
So, as per the above sequence, you should correct your query like below. Also, you should place double quote(") correctly.
$recent_video = $db->query("
SELECT video_id
FROM video_primary
WHERE userid = '$userid'
ORDER BY timestamp DESC LIMIT 1"
) or die($db->error);

Yii: Select 20 last entries order by id ASC

I would like to get the 20 last entries of my table but order by ascending id.
In Sql it's not very complicated:
SELECT *
FROM (SELECT * FROM comments
WHERE postID='$id'
ORDER BY id DESC
LIMIT 20) t
ORDER BY id ASC;
But I would like to to it with my yii model like:
Comment::model()->findAll($criteria)
But i really don't know what I should put in my CDbCriteria!
$models = Comment::model()->findAll(array(
"condition" => "WHERE postID = '".$id."'",
"order" => "id DESC",
"limit" => 20,
));
Will get the last 20. And now you want to order that record set by id ASC correct? Is there not another field you can order by for a similar result (maybe a date or created field?) eg:
"order" => "id DESC, created ASC"
Scrap that secondary ordering, but why not just use array reverse?
$models = array_reverse($models);
There is a way without using array_reverse, if you think of using this sql:
SELECT * FROM `comments` `t`
WHERE id
in (SELECT id
FROM (SELECT id FROM comments Where postID = xyz ORDER BY id DESC LIMIT 20)
as q)
ORDER BY id ASC
which in criteria will become:
$criteria=new CDbCriteria();
$criteria->condition='id in (SELECT id FROM (SELECT id FROM comments Where postID='.$id.' ORDER BY id DESC LIMIT 20) as q)';
$criteria->order='id ASC';
Update:
With your original query, you could have also used findBySql :
$sql='SELECT * FROM (SELECT * FROM comments WHERE postID= :postid ORDER BY id DESC LIMIT 20) q ORDER BY id ASC';
$params=array('postid'=>$id);
$comments=Comment::model()->findAllBySql($sql,$params);
The performance of this query was better than my previous query.
UPD:
Please note, that in general, some other solutions are better than mine.
Using offset can decrease performance of your queries.
See: http://www.slideshare.net/Eweaver/efficient-pagination-using-mysql and Why does MYSQL higher LIMIT offset slow the query down?
So, when the number of Comments will increase, you can get performance degradation.
What about using offset feature?
$model = Comment::model();
$condition = 'postID =' . $id;
$limit = 20;
$totalItems = $model->count($condition);
$criteria = new CDbCriteria(array(
'condition' => $condition,
'order' => 'id ASC',
'limit' => $limit,
'offset' => $totalItems - $limit // if offset less, thah 0 - it starts from the beginning
));
$result = $model->findAll($criteria);

Get Latest Entry from Database

How can I get the latest entry by the latest DATE field from a MySQL database using PHP?
The rows will not be in order of date, so I can't just take the first or last row.
You want the ORDER BY clause, and perhaps the LIMIT clause.
$query = 'SELECT * FROM `table` ORDER BY `date` DESC LIMIT 1';
SELECT * FROM [Table] ORDER BY [dateColumn] DESC
If you want only the first row:
In T-SQL:
SELECT TOP(1) * FROM [Table] ORDER BY [dateColumn] DESC
In MySQL:
SELECT * FROM `Table` ORDER BY `dateColumn` DESC LIMIT 1
You don't have a unique recordid or date revised field you could key in on? I always have at least an auto incrementing numeric field in addition to data created and date revised fields. Are you sure there is nothing you can key in on?
SELECT * FROM table ORDER BY recno DESC LIMIT 1;
or
SELECT * FROM table ORDER BY date_revised DESC LIMIT 1;
So the PHP call would be:
$result = mysql_query("SELECT * FROM table ORDER BY date_revised DESC LIMIT 1");
-- Nicholas
You can use a combination of the LIMIT and ORDER BY clauses.
For example:
SELECT * FROM entries ORDER BY timestamp DESC LIMIT 1

Categories