Select all rows except three in using MySQL php - php

I was trying to use a select statement to get all the rows from a certain MySQL table except for three which has in user_id of 5,6,7. Below is the code but its not working properly. Please can you help.
$sql = "SELECT * FROM login ORDER BY user_id ASC LIMIT 0, 20 WHERE user_id<>5,6,7";

You have to Use NOT IN function for multiple id's.
$sql = "SELECT * FROM login WHERE user_id NOT IN (5,6,7) ORDER BY user_id ASC LIMIT 0, 20"

Do it like this:
SELECT * FROM login WHERE NOT user_id = 5, ORDER BY user_id ASC LIMIT 0, 20

$sql = "SELECT * FROM login WHERE user_id<>5 ORDER BY user_id ASC LIMIT 0, 20 "
Look at Order by, Where Clause and Order of operations
As you've just changed your question the new answer is
$sql = "SELECT * FROM login WHERE user_id NOT IN (5,6,7) ORDER BY user_id ASC LIMIT 0, 20 "
Look at NOT IN

Try this:
$sql = "SELECT * FROM `login` WHERE `user_id` NOT IN (5,6,7) ORDER BY `user_id` ASC LIMIT 0, 20";

Related

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");

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

SQL query with IN clause

I am working on a page, thats like facebook feed page.
So, my query now look like that:
$sql = "SELECT *
FROM event
WHERE id_user IN (
SELECT a.id_user_stalkers
FROM stalkers a
WHERE a.id_user = ".$id_profile.")
ORDER by date DESC
LIMIT 0, 10";
stalkers - friends
Table Stalkers:
id id_user id_user_friend
I need to append to that IN clause my personal ID, so my qyery returns my events and my friends event.
Can anybody help me ?
What I have tried
$sql = "SELECT *
FROM event
WHERE id_user IN (
SELECT a.id_user_stalkers
FROM stalkers a
WHERE a.id_user = ".$id_profile.")
OR id_user = ".$id_profile."
ORDER by date DESC
LIMIT 0, 10";
it looks something wrong in your sql and your table
1-in your table you have id_user_friend and in your sql you are making id_user_stalkers.
2- i dont know if you have date in your first table , or you mean NOW() .? since u didnt share the first table.
try this
$sql = "SELECT * FROM event e WHERE id_user
IN (SELECT a.id_user_friend FROM stalkers a
WHERE a.id_user = ".$id_profile.")
OR e.id_user = ".$id_user."
ORDER by e.date DESC LIMIT 0, 10";
What is $id_user ? The original query only has one variable. I don't see why you need another. In other words, try this:
SELECT *
FROM event e
WHERE id_user IN (SELECT a.id_user_friend
FROM stalkers a
WHERE a.id_user = ".$id_profile."
) OR
e.id_user = ".$id_profile."
ORDER by e.date DESC LIMIT 0, 10

MYSQL Select from table, get newest/last 10 rows in table

What's the best, and easiest way to do this? My query currently is:
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id
LIMIT 10
This shows the first 10 rows though, not the last 10.
EDIT: I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order.
to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC
EDIT
Based on your comment:
SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC
If you want the last 10 then just change ASC to DESC
SELECT *
FROM
chat
WHERE
(userID=$session AND toID=$friendID)
OR
(userID=$friendID AND toID=$session)
ORDER BY id
DESC
LIMIT 10
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$limit = 10;
$query = "SELECT * FROM $table";
$resource = mysqli_query($con,$query);
$total_rows = mysqli_num_rows($resource);
$start = $total_rows-$limit;
$query_limit= $query." LIMIT $start,$limit";
First I have set the limit
$limit = 10;
then
$total_rows = mysqli_num_rows($resource);
Here I have taken total number of rows affected.
$start = $total_rows-$limit;
then substracted limit from number of rows to take starting record number
$query_limit= $query." LIMIT $start,$limit";
and then added limit to the query.
For more information about limit see this link
https://www.w3schools.com/php/php_mysql_select_limit.asp
First select the last 10 from the table, then re-order them in ascending order.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 10) sub ORDER BY id ASC

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