PHP: MYSQLI Change Order By Style - php

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

Related

Query wildcard with IN

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

How i can reverse mysqli querie's result order

I don't know the title is relevant enough or not..
Here's the thing i wanna do
i have a table (tbl_users) with two columns
id(Primary)
name(varchar)
with entries..Like this...
when i execute this query
$result=mysqli_query($conn,"select * from tbl_users order by id desc limit 3");
while($row=mysqli_fetch_array($result)){
echo $row["id"]."<br>";
}
and i get these results
7
6
5
But I want result in this order
5
6
7
Edit:
According to your comment, here another try:
$result=mysqli_query($conn,"SELECT * FROM ( SELECT * FROM tbl_users ORDER BY id DESC LIMIT 3) as r ORDER BY id asc");
when you are using order by in mysql you write column for order and (ASC or DESC)
ASC-ascending
DESC- desceding
if you dont put asc or desc, default is asc, in your case its like this
ORDER BY id or
ORDER BY id ASC
$result=mysqli_query($conn,"select * from tbl_users order by id asc limit 3 OFFSET 4");
Try
$result=mysqli_query($conn,"select * from tbl_users order by id ASC limit 3 OFFSET 4");

Select last 5 mysql rows

i want to get the last 6 rows but skip the last one in result:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 6
How can i make this?
Try this:
SELECT *
FROM (SELECT *
FROM TableName
ORDER BY FieldName DESC
LIMIT 5) sub
ORDER BY FieldName ASC
Try this:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 1,6

Php count number of how many changed = y where humen=yes

I tried something like this:
$changed = mysql_query("SELECT * FROM games WHERE changed = 'y' AND human = '$_GET[human]' ORDER BY id DESC LIMIT 100", $link);
$num_rowsc = mysql_num_rows($changed);
So what i want is to select last 100 where human = yes and count where changed = y ..
so get last 100 humans and count how many of them have on changed yes
Are you looking for something like this :
$stmt = $pdo->prepare("SELECT COUNT(*) AS cnt FROM ( SELECT * FROM games
WHERE human= :human ORDER BY id DESC LIMIT 100
) WHERE changed = 'y' ");
$stmt->bindParam(':human', $_GET[human]);
$stmt->execute();
Maybe with this query?
SELECT COUNT(*) AS cnt FROM (
SELECT * FROM games
WHERE human = '".mysql_real_escape_string($_GET[human])."'
ORDER BY id DESC LIMIT 100
) tmp WHERE changed = 'y'
1) try using this as your query:
SELECT count(*) FROM (SELECT * FROM games WHERE changed = 'y' ORDER BY id DESC LIMIT 100) WHERE human = '$_GET[human]'
2) use mysqli
select count(*) as cnt from (
SELECT human FROM games WHERE human = '$_GET[human]' ORDER BY id DESC LIMIT 100
) as changes WHERE changed = 'y'

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