+----+
| id |
+----+
| 1 |
+----+
| 2 |
+----+
| 3 |
+----+
| 4 |
+----+
+----+
| 5 |
+----+
How do I show records from a specific ID so the list would show 2,3,4,5 or 3,4,5?
I figured to do with two queries in UNION but end up showing 2,1,3,4,5 or 2,5,4,3,1.
Did you mean
SELECT * FROM table WHERE id IN (2,3,4,5)
SELECT * FROM table WHERE id >= $id ORDER BY id ASC LIMIT 4
In the second query $id is the input from the user. Also instead of LIMIT 4, you can have the 4 taken as user input
Maybe
$sql = "SELECT * FROM table WHERE id >= $curID ORDER BY id ASC LIMIT 4";
If you want to specify how many entries to find, you can use:
$sql = "SELECT * FROM table WHERE id >= $curID ORDER BY id ASC LIMIT $number";
Be sure you sanitize the inputs before plugging them into the query. See this post for more info:
What's the best method for sanitizing user input with PHP?
Related
I have a table with many columns. Each row contains a unique ID field. How can I select 5 rows without selecting a row with id stored in variable $doNot.
Currently I am using the following query.
$qry="SELECT title, link FROM posts WHERE id > (SELECT MAX(id) - 5 FROM news)";
Consider this table:
id | title | link
_____________________________________
1 | title_1 | link_1
2 | title_2 | link_2
3 | title_3 | link_3
4 | title_4 | link_4
5 | title_5 | link_5
6 | title_6 | link_6
7 | title_7 | link_7
8 | title_8 | link_8
I want to select 5 rows except row with id '3' using the above query from the above table
SELECT title, link FROM posts WHERE NOT (id = $doNot) LIMIT 5
Use LIMIT to specify the number of rows to be returned:
SELECT title, link
FROM posts
ORDER BY ID DESC
LIMIT 5
If $doNot is a table variable, you should be able to simply sub-query on posts.id:
SELECT title, link FROM posts WHERE id NOT IN (SELECT id FROM $doNot) LIMIT 5;
I have a huge number of rows that I'd like to get say, last 5 records inserted in that database from 10 different users. If the same user inserted the last 3 rows into database, we must get one row, skip the others two and move to get a row per user, until it count up to 5.
A database like that:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
1 | 4 | baa
4 | 5 | baa0
5 | 6 | baa1
5 | 7 | baa2
6 | 8 | baa3
7 | 9 | baa4
Should return:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
4 | 5 | baa0
5 | 6 | baa1
The current filter was done by PHP, like this:
$used = array();
while ($data = mysql_fetch_array($query)) {
$uid = $data['user_id'];
if(in_array($uid, $used))
continue;
array_push($used, $uid);
// do something with data
}
But I want to refactor it, and do the filter purely by mysql, if possible. I don't know much MySql and that's why I'm having problem to archive this...
Here's what I've tried
select DISTINCT(user_id), news_id, title from XXX
WHERE GROUP BY (news_id) DESC
LIMIT 0,5
How can I do that?
1 way you can do it is to generate a partitioned row number per user and then select 5 records where RowNumber = 1.
SELECT *
FROM
(
SELECT
d.user_id
,d.news_id
,d.title
,(#rn:= if(#uid = user_id, #rn + 1,
if(#uid:=user_id,1,1)
)
) as RowNumber
FROM
Data d
CROSS JOIN (SELECT #uid:=-1, #rn:=0) vars
ORDER BY
user_id
,news_id
) t
WHERE
t.RowNumber = 1
ORDER BY news_id
LIMIT 5;
http://rextester.com/JRIZI7402 - example to show it working
Note you can change the row order by simply changing the ORDER BY statement of the derived table so if you have a column that will signify the latest record e.g. an identity column or a datetime column you can use that, but user_id must be the first criteria to be partitioned correctly.
Do it from your query.
"SELECT * FROM table GROUP BY user_id ORDER BY news_id DESC LIMIT 5"
well, i think this will achieve what you are after.
select user_id, news_id, title from tableName
GROUP BY user_id
ORDER BY news_id DESC
LIMIT 0,5
Hope this helps!
ID | NAME
1 | yassine
2 | yask
3 | oisus
4 | iouosiud
5 | iouous
$query = mysql_query("SELECT * FROM table ORDER BY id DESC");
what i want to do is that i want to start with 3 . It should output like this:
3,1,2,4,5.
is it possible ?
This works:
SELECT * FROM table ORDER BY id=3 DESC, id ASC
Arranged the list using ORDER BY.
SELECT * FROM `table` ORDER BY (id=3) ASC
No need to use union!
i have the lot of data and i have to short data that has on Approval and Approval data with
short id descending.
Like the following query:
$query = "select * from event ORDER BY FIELD(status, 'Approval') desc";
But when new data added in event table. as when new data added in table its status is on Approval. and when i show the data .data is not sorted with event id descending.
I am confused how to use both order by field and order by id descending please help
Thanks and Regards
You can do as following if you want the Approval to appear first and then by descending order of id
select * from event
ORDER BY FIELD(status, 'Approval') desc ,event_id desc
Here is a real time example
mysql> select idusers from users where status = 'paid' order by idusers desc limit 1 ;
+---------+
| idusers |
+---------+
| 150949 |
+---------+
1 row in set (0.00 sec)
Above is to get the most recent paid user
mysql> select idusers,status from users order by field (status,'paid')desc ,
idusers desc limit 3;
+---------+--------+
| idusers | status |
+---------+--------+
| 150949 | paid |
| 150948 | paid |
| 150947 | paid |
+---------+--------+
In the above the most recent is the first row followed by others.
I have a database like this:
+----------+----------+------+
| username | password | time |
+----------+----------+------+
| a | b | 1234 |
| c | d | 5678 |
| e | f | 9012 |
+----------+----------+------+
Now I have to arrange this by time so I ran:
mysql_query("SELECT * FROM `table` ORDER BY `time`")
This showed me rows arranged by time in ascending order, but I have to get only username c and e or I have to get the last two rows from the query.
I have tried:
mysql_query('SELECT * FROM `table` ORDER BY `time` LIMIT 2')
But this showed me usernames a and c but I have to get the last two, how to do that?
SELECT * FROM table
ORDER BY time DESC
LIMIT 2
SELECT * FROM table ORDER BY time DESC LIMIT 2
Adding the DESC keyword will sort the results in descending order.
Try this:
SELECT * FROM table ORDER BY time DESC LIMIT 2
Try
mysqli_query('SELECT * FROM table ORDER BY time DESC LIMIT 2')
Assuming you have an id field with AUTO_INCREMENT set.
To get the last two from the table.
SELECT * FROM table
ORDER BY time DESC
LIMIT 2