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;
Related
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!
Hello there, I have a schema like this, table name feeds
Where msg_id is unique and its the primary key of the table
| msg_id |commented|
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 21 |
I want to build a query that would select the last two rows
The output should go like this
| msg_id |commented|
| 3 | 10 |
| 4 | 21 |
In short the query should return the rows with msg_id which have a distinct commented value
Group by the column ment to be unique and select the highest id for every group
select max(msg_id) as msg_id, commented
from your_table
group by commented
Try this -
select max(msg_id), commented from your_table group by commented
SELECT * FROM feeds GROUP BY commented
There's a little thing I'm stuck with. I've a table in my database like:
id | AuthorId | Book
---------------------
1 | 2 | book name
2 | 2 | book name
3 | 2 | book name
4 | 2 | book name
5 | 5 | book name
6 | 5 | book name
7 | 8 | book name
8 | 9 | book name
9 | 9 | book name
10 | 6 | book name
As you can see, Author ID "2" is repeated the highest times in this table ( 4 times ) while the author Author IDs appeared less then 4 times. How can I get the ID "2" from this table using php MySQLi, which is the most frequently occurring value in this data? I've no idea how can I do this.
Thank you very much.
Try the following query. You count rows grouped by AuthorID DESC , and limiting the result to the top value.
SELECT AuthorId, COUNT(AuthorId) as c from table
GROUP BY AuthorId
ORDER BY COUNT(AuthorId) DESC
LIMIT 1;
Try this
select AuthorId , count(AuthorId ) as max
from table_name
group by AuthorId order by max desc limit 1;
The order by max desc is for ordering the max value at the first row. The limit 1 is from getting only the first row
we re using CodeIgniter, we want to get 6 categories latest 10 entries by (published_date), then we will display different categories results in view. here we are using 6 colourful boxes to display 10 latest entries from each category. our SQL tables are look like this ...
-> ci_categories
cat_id | cat_name | cate_slug | cate_title
-> ci_pages
page_id | cat_id | page_title | published_date
--------------------------------------
1 | 1 | Ttl 1 | 2014-02-22 10:22:20
2 | 2 | Ttl 2 | 2014-02-24 11:42:30
3 | 1 | Ttl 3 | 2014-02-26 10:37:21
4 | 3 | Ttl 3 | 2014-02-28 12:40:30
SELECT `ci_pages`.`cat_id` AS CAT_ID,
(SELECT `cat_name` FROM `ci_categories` WHERE `ci_categories`.`cat_id` = `ci_pages`.`cat_id`) AS CAT_NAME,
(SELECT `cat_slug` FROM `ci_categories` WHERE `ci_categories`.`cat_id` = `ci_pages`.`cat_id`) AS CAT_SLUG,
(SELECT `cat_title` FROM `ci_categories` WHERE `ci_categories`.`cat_id` = `ci_pages`.`cat_id`) AS CAT_TITLE
FROM `ci_pages`
ORDER BY `ci_pages`.`published_date` DESC
LIMIT 10;
This will return the 10 last inserted values into "ci_pages" Table getting each Category's details from "ci_pages" table. Check now...
try this query..
select category.*,pages.* from ci_categories category join ci_pages pages on (category.cat_id = pages.cat_id) group by pages.cat_id order_by pages.published_date DESC limit 0,10
I have this table: I want to search by UID
ID | VID | UID
1 | 1 | 5
1 | 1 | 6
1 | 2 | 6
2 | 3 | 5
2 | 3 | 6
2 | 4 | 6
I want to end up with this result:
ID | VID | UID
1 | 2 | 6
2 | 4 | 6
In other words, only select the entries where the VID is MAX of the UID but keeping in min NID could differ. Something like this I suppose:
select * from TABLE where uid = 6 and max(vid)
???
But this doesn't work?
One way is to order by the value in descending order (so the max is at the top), then just select the first result.
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
ORDER BY t.VID DESC
LIMIT 1
Or do you mean you want all rows where t.VID is the highest value? In which case you could do something like this,
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
AND t.VID = (SELECT MAX(VID) FROM table);
EDIT: Based on the edit to your question, it looks like you just want the max VID value for each ID? If I'm understanding you correctly, then this should give you what you need.
SELECT t.ID,
max(t.VID) as VID,
t.UID
FROM table t
WHERE t.UID = 6
GROUP BY t.ID
You need to have a subquery. This should work:
select * from TABLE where ID='1' AND VID=(select max(VID) from TABLE)
I expect your real-life example is more complicated (at least has more data).
This query will give you the row you want.
SELECT id,vid, uid
FROM TABLE
where id = 1
and vid in (select max(vid) from TABLE where id = 1)