multiple subquery result into main query, select in select - mysql

OK have 2 tables
user_id login_history
1 2011-01-01
1 2011-01-02
1 2011-03-05
1 2011-04-05
1 2011-06-07
2 2011-01-01
2 2011-01-02
3 2011-03-05
3 2011-04-05
3 2011-06-07
user_id user_details
1 Jack
2 Jeff
3 Irin
What kind of query can I use to get a result like
1. Jack 2011-01-01 2011-01-02 2011-03-05
2. Jeff 2011-01-01 2011-01-02
3. Irin 2011-03-05 2011-04-05 2011-06-07
Basically I want latest 3 records from table one and be joint with table 2
The query I used will get me a list of below, which is vertical records
Jack ,2011-01-01
Jack ,2011-01-02
Jack ,2011-03-05
Jeff ,2011-01-01
Jeff ,2011-01-02
Irin ,2011-03-05
Irin ,2011-04-05
Irin ,2011-06-07
Please help

select t2.user_details,
substring_index(group_concat(login_history order by login_history separator ' '),' ',3) as recents
from table_2 as t2
left join table_1 as t1
on t1.user_id = t2.user_id
group by t2.user_id
in your example you list first three records, not the last three. By the way you would have just to add desc to the order clause within group_concat if you need it.

Related

Select from two tables but only for the maximum values in one of the columns

I have the following two tables. I want to select all the users with user_type 1, as well as the details for their most recent note. I want to order the results by the note time_created.
users table:
user_id name user_type
1 Joe 1
2 Fred 1
3 Sam 0
4 Dave 0
notes table:
notes_id user_id note time_created
1 1 Joe 1 2019-06-05 13:45:00
2 1 Joe 2 2019-06-05 13:46:00
2 1 Joe 3 2019-06-05 13:47:00
3 2 Fred 1 2019-06-05 13:45:00
4 2 Fred 2 2019-06-05 13:46:00
5 3 Sam 1 2019-06-05 13:45:00
6 4 Dave 1 2019-06-05 13:45:00
So the result of the query would be
user_id name note time_created
1 Joe Joe 3 2019-06-05 13:47:00
2 Fred Fred 2 2019-06-05 13:45:00
This is my effort so far
SELECT users.user_id, users.name, notes.note, notes.time_created FROM notes
INNER JOIN users ON users.id=prospect_notes.subject_id
WHERE users.user_type = 1 ORDER BY notes_time_created;
It is pretty good, but it returns several rows per user. I just want the one row containing the maximum time created.
You can use a correlated subquery:
SELECT u.user_id, u.name, n.note, n.time_created
FROM users u JOIN
notes n
ON u.id = n.subject_id
WHERE u.user_type = 1 AND
n.notes_time_created = (SELECT MAX( n2.notes_time_created )
FROM notes n2
WHERE n2.subject_id = n.subject_id
)
ORDER BY n.notes_time_created;

MySQL Join Three Tables, Count, and Get The Last Username

I have a problem with sql query. I need join three tables, count rows, get last row, and the create custom column.
The example my tables like below:
table name: article
_______________________________________
idarticle idwriter title
---------------------------------------
1 1 Title One
2 3 Title Two
3 2 Title Three
table name: comment
________________________________________________________________________________
idcomment idarticle idcommented content datetime
--------------------------------------------------------------------------------
1 1 2 Comment One 2015-05-15 00:00:00
2 1 3 Comment Two 2015-05-16 00:00:00
3 1 1 Comment Three 2015-05-17 00:00:00
4 2 2 Comment Four 2015-05-18 00:00:00
5 3 3 Comment Five 2015-05-19 00:00:00
6 3 2 Comment Six 2015-05-20 00:00:00
table name: member
_____________________
idmember username
---------------------
1 apple
2 orange
3 banana
How to join all tables, count, and get last commented+username with one query.
May the result like:
_____________________________________________________________________________________________________________________
idarticle idwriter title username_writer totalcomments lastcomment_id lastcomment_username lastcomment_datetime
---------------------------------------------------------------------------------------------------------------------
3 2 Title Three orange 2 2 orange 2015-05-20 00:00:00
2 3 Title Two banana 1 2 orange 2015-05-18 00:00:00
1 1 Title One apple 3 1 apple 2015-05-17 00:00:00
I hope someone will solve my problem.
I using PHP 5.4, MySQL 5.5, and MeekroDB library, so the query must support with those.
Sorry for my English.
Thank you
http://sqlfiddle.com/#!9/132336/1
SELECT a.idarticle, a.idwriter , a.title,
m.username,
c.idcomment, cm.username, c.datetime
FROM article a
LEFT JOIN member m
ON a.idwriter = m.idmember
LEFT JOIN comment c
ON a.idarticle = c.idarticle
LEFT JOIN comment c1
ON a.idarticle = c1.idarticle
AND c.datetime<c1.datetime
LEFT JOIN member cm
ON c.idcommented = cm.idmember
WHERE c1.datetime IS NULL

Sorting mysql by number of occurrences

How do you order a mysql table by the number of occurrences in a spesific column?
Example table:
ID Name
1 Alfred
2 Alfred
3 Burt
4 Alfred
5 Jill
6 Jill
7 Jill
8 Jill
9 Burt
The sorted table should be like below, since "Jill" is the name occurring most, it should be sorted first, and so on:
ID Name
5 Jill
6 Jill
7 Jill
8 Jill
1 Alfred
2 Alfred
4 Alfred
3 Burt
9 Burt
You have to bring in the information into the query. This is typically done using a join:
select e.*
from example e join
(select name, count(*) as cnt
from example
group by name
) en
on e.name = en.name
order by cnt desc, e.name, e.id;
Note that the order by not only orders by the count. It also orders by the name. If two names have the same count, then it will keep them together.

mysql select most recent, limit by source

I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.
Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...
select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.