Sql select order by other table - mysql

I want to order "contact" table by the "contactreply" table.
The "contactid" row in "contactreply" table is the "ID" row in the "contact" table. "Contact" has to be ordered by the last insert in the table "contactreply" where the "contactid" is the "ID" in the table "contact".
contact table:
+----+--------+----------+----------+
| ID | userid | subject | content |
+----+--------+----------+----------+
| 1 | 1 | subject | subject |
| 2 | 1 | subject2 | subject2 |
+----+--------+----------+----------+
reply table
+----+--------+-----------+---------+
| ID | userid | contactid | content |
+----+--------+-----------+---------+
| 1 | 1 | 1 | reply1 |
| 2 | 1 | 2 | reply2 |
| 3 | 1 | 1 | fasd |
| 4 | 1 | 2 | asdf |
| 5 | 1 | 2 | f |
| 6 | 1 | 1 | asdf |
+----+--------+-----------+---------+
I tried my best to explain it and hope you understand it ;)
I allready looked at the other posts but I can't get it to work.
Thank you in advance.

SELECT c.*
FROM contacts c
JOIN reply x
ON x.contactid = c.id
JOIN
( SELECT MAX(id) max_id FROM reply GROUP BY contactid ) y
ON y.max_id = x.id
ORDER
BY x.id;
+----+--------+----------+----------+
| ID | userid | subject | content |
+----+--------+----------+----------+
| 2 | 1 | subject2 | subject2 |
| 1 | 1 | subject | subject |
+----+--------+----------+----------+

Check if this is what you expect
select a.*, b.id from contact as a
inner join
(select * from contactReply as x
where x.id =
(select max(id) from contactReply as y
where x.contactid = y.contactid)
) as b
on a.ID = b.contactId
order by b.id

Related

selecting table per type?

I have a problem on selecting different table of user_details by their user_ID from account table,
Account table
+---------+------------+---------------+------------+
| user_ID | user_Name | user_Password | user_Level |
+---------+------------+---------------+------------+
| 1 | student | student | 1 |
| 2 | instructor | instructor | 2 |
| 3 | admin | admin | 3 |
+---------+------------+---------------+------------+
Student_details
+---------+----------+--------------+
| stud_ID | user_ID | stud_details |
+---------+----------+--------------+
| 1 | 1 | student |
| 2 | null | student2 |
| 3 | null | student3 |
+---------+----------+--------------+
Instructor_details
+---------+---------+--------------+
| inst_ID | user_ID | inst_details |
+---------+---------+--------------+
| 1 | null | instructor |
| 2 | 2 | instructor2 |
| 3 | null | instructor3 |
+---------+---------+--------------+
and the Admin_details same as detail table from above..
heres my sample query:
SELECT
ua.user_ID,
ul.level_Name,
ua.user_Name,
ua.user_status,
(CASE
WHEN ua.level_ID = 1 THEN (SELECT * FROM `record_student_details` rsd WHERE rsd.user_ID = ua.user_ID)
WHEN ua.level_ID = 2 THEN "2"
WHEN ua.level_ID = 3 THEN "3"
ELSE "error"
END) as ulvl FROM `user_accounts` ua
LEFT JOIN user_level ul ON ua.level_ID = ul.level_ID
the result i wanted is like this, if user_level is 1 the detail of user must came from student_details, then if user_level is 2 his detail will be instructor_details then, if user_level is 3 then detail from admin_details
+---------+------------+---------------+------------+--------------------+
| user_ID | user_Name | user_Password | user_Level | details |
+---------+------------+---------------+------------+--------------------+
| 1 | student | student | 1 | Student_details |
| 2 | instructor | instructor | 2 | Instructor_details |
| 3 | admin | admin | 3 | Admin_details |
+---------+------------+---------------+------------+--------------------+
You seem to want something like this:
SELECT ua.user_ID, ul.level_Name, ua.user_Name, ua.user_status,
COALESCE(stud_id, inst_id) as id,
COALESCE(stud_details, inst_details) as details
FROM user_accounts ua LEFT JOIN
user_level ul
ON ua.level_ID = ul.level_ID LEFT JOIN
student_details sd
ON sd.user_ID = ua.user_ID AND ua.level_ID = 1 LEFT JOIN
student_details sd
ON id.user_ID = ua.user_ID AND ua.level_ID = 2

MySql Left Join records where is null with handling validUntil Row

How to select all addons which a specific user has not buyed or are no longer valid?
Assuming currentdate is 2017-03-02 17:00:00
Table1 (users):
+-----------+----------+
| id | username |
+-----------+----------+
| 1 | Walter |
| 2 | Hank |
| 3 | John |
+-----------+----------+
Table2 (buyLog):
+-----------+----------+------------+---------------------+
| id | idUsers | idItems | validUntil |
+-----------+----------+------------+---------------------+
| 1 | 1 | 1 | 2016-03-02 14:15:47 |
| 2 | 1 | 1 | 2018-03-02 14:15:47 |
| 3 | 1 | 2 | 2016-03-02 14:15:47 |
| 4 | 2 | 1 | 2018-03-02 14:15:47 |
+-----------+----------+------------+---------------------+
Table3 (addons):
+-----------+----------+
| id | name |
+-----------+----------+
| 1 | Foo |
| 2 | Bar |
| 3 | Lorem |
+-----------+----------+
Expected output for user with id 1 should be:
+-----------+----------+
| id | name |
+-----------+----------+
| 2 | Bar |
| 3 | Lorem |
+-----------+----------+
See SQL Fiddle here: http://sqlfiddle.com/#!9/16356
Where I have the most problems is to handle the validUntil in the leftJoin.
I think I have to group by during the left join to tread only the most recent validUntil record. Maybe using max(validUntil)?
This code will work
http://sqlfiddle.com/#!9/16356/1/0
SELECT
C.ID AS 'ID',
C.NAME AS 'NAME'
FROM
(SELECT
A.id AS 'ID',A.name AS 'NAME',
CASE
WHEN B.YY > '2017-03-02 17:00:00' THEN 0
ELSE 1 END AS 'Tag'
FROM
addons AS A
LEFT JOIN
(SELECT idItems AS 'XX', MAX(validUntil) AS 'YY'
FROM
buyLog
WHERE idUsers = 1 GROUP BY 1) AS B
ON
A.id = B.XX) AS C
WHERE
C.Tag = 1
My sense is that neither your explanation nor your data set and desired result are adequate to the task of explaining the problem. The following query produces the desired result, but perhaps that's just coincidence...
SELECT a.*
FROM addons a
LEFT
JOIN buylog b
ON b.iditems = a.id
AND b.validuntil > NOW()
LEFT
JOIN users u
ON u.id = b.idusers
AND u.id = 1
WHERE b.validuntil IS NULL
AND u.id IS NULL;

How can I determine which user is the top one in the specific tag?

I have a question and answer website like stackoverflow. Here is the structure of some tables:
-- {superfluous} means some other columns which are not related to this question
// q&a
+----+-----------------+--------------------------+------+-----------+-----------+
| id | title | body | type | related | author_id |
+----+-----------------+--------------------------+------+-----------+-----------+
| 1 | How can I ... | I'm trying to make ... | q | NULL | 3 |
| 2 | | You can do that by ... | a | 1 | 1 |
| 3 | Why should I .. | I'm wonder, why ... | q | NULL | 1 |
| 4 | | First of all you ... | a | 1 | 2 |
| 5 | | Because that thing ... | a | 3 | 2 |
+----+-----------------+--------------------------+------+-----------+-----------+
// users
+----+--------+-----------------+
| id | name | {superfluous} |
+----+--------+-----------------+
| 1 | Jack | |
| 2 | Peter | |
| 3 | John | |
+----+--------+-----------------+
// votes
+----+----------+-----------+-------+-----------------+
| id | user_id | post_id | value | {superfluous} |
+----+----------+-----------+-------+-----------------+
| 1 | 3 | 4 | 1 | |
| 2 | 1 | 1 | -1 | |
| 3 | 2 | 1 | 1 | |
| 4 | 3 | 2 | -1 | |
| 5 | 1 | 4 | 1 | |
| 6 | 3 | 5 | -1 | |
+----+--------+-------------+-------+-----------------+
// tags
+----+------------+-----------------+
| id | name | {superfluous} |
+----+------------+-----------------+
| 1 | PHP | |
| 2 | SQL | |
| 3 | MySQL | |
| 4 | HTML | |
| 5 | CSS | |
| 6 | C# | |
+----+------------+-----------------+
// q&aTag
+-------+--------+
| q&aid | tag_id |
+-------+--------+
| 1 | 1 |
| 1 | 4 |
| 3 | 5 |
| 3 | 4 |
| 4 | 6 |
+-------+--------+
Now I need to find top users in a specific tag. For example, I need to find Peter as top user in PHP tag. Because his answer for question1 (which has PHP tag) has earned 2 upvotes. Is doing that possible?
Try this:
select q1.title, u.id, u.name, sum(v.value) total from `q&a` q1
left join `q&atag` qt ON q1.id = qt.`q&aid`
inner join tags t ON qt.tag_id = t.id
left join `q&a` q2 ON q2.related = q1.id
left join users u ON q2.author_id = u.id
left join votes v ON v.post_id = q2.id
where t.name = 'PHP'
group by q1.id, u.id
and here is a simple divided solution:
Let us divide it into sub queries:
get the id of the tag you will search for: select id from tags where name = 'PHP'
get the questions with this tag: select 'q&aid' from 'q&aTag' where tag_id = 1.
get the ids of answers for that question: select id, author_id fromq&awhere related in (2.)
get the final query: select user_id, sum(value) from votes where post_id in (3.) group by user_id
Now combining them all give the result:
select user_id, sum(`value`) total from votes
where post_id in (
select id from `q&a` where related in (
select `q&aid` from `q&aTag` where tag_id IN (
select id from tags where name = 'PHP'
)
)
)
group by user_id
you can add this at the end if you want only one record:
order by total desc limit 1

MySql Left Join with subselect

I have 2 tables (user and infos).
I need to select all user data and the related last inserted 'infotext' (insert_time)
table user
+----+--------+----------+
| id | name | adress |
+----+--------+----------+
| 1 | Name 1 | Adress 1 |
| 2 | Name 2 | Adress 2 |
| 3 | user 3 | adress 3 |
| 4 | user 4 | adress 4 |
+----+--------+----------+
table infos
+----+---------+----------+---------------------+
| id | id_user | infotext | insert_time |
+----+---------+----------+---------------------+
| 1 | 1 | info 1 | 2016-11-24 14:03:23 |
| 2 | 1 | info 2. | 2016-11-24 14:08:30 |
| 3 | 3 | text 3. | 2016-11-24 14:08:46 |
+----+---------+----------+---------------------+
My current query is:
SELECT a.*, b.infotext FROM user a LEFT JOIN infos b
ON a.id = b.id_user
LEFT JOIN
(
SELECT id_user, MAX(insert_time) newestInsert
FROM infos
GROUP BY id_user
) c ON c.id_user = b.id_user AND
c.newestInsert = b.insert_time
But the problem is it outputs the id not distinct:
+----+--------+----------+----------+
| id | name | adress | infotext |
+----+--------+----------+----------+
| 1 | Name 1 | Adress 1 | info 1 |
| 1 | Name 1 | Adress 1 | info 2. |
| 3 | user 3 | adress 3 | text 3. |
| 2 | Name 2 | Adress 2 | NULL |
| 4 | user 4 | adress 4 | NULL |
+----+--------+----------+----------+
The final result I need is:
+----+--------+----------+----------+
| id | name | adress | infotext |
+----+--------+----------+----------+
| 1 | Name 1 | Adress 1 | info 2. |
| 3 | user 3 | adress 3 | text 3. |
| 2 | Name 2 | Adress 2 | NULL |
| 4 | user 4 | adress 4 | NULL |
+----+--------+----------+----------+
Put the second condition in the on clause. This method does it as a correlated subquery:
SELECT u.*, i.infotext
FROM user u LEFT JOIN
infos i
ON u.id = i.id_user and
i.insert_time = (SELECT MAX(i2.insert_time)
FROM infos i2
WHERE i2.id_user = i.id_user
);
If performance is key...
SELECT u.id
, u.name
, u.adress
, i.infotext
FROM user u
LEFT
JOIN
( SELECT x.*
FROM infos x
JOIN
( SELECT id_user
, MAX(insert_time) insert_time
FROM infos
GROUP
BY id_user
) y
ON y.id_user = x.id_user
AND y.insert_time = x.insert_time
) i
ON i.id_user = u.id
ORDER
BY infotext IS NULL, infotext;

group Items by column and order by other column

I have table as below , I want to take latest rating for the client
basically user whenever updates rating, count will be incremented and a entry will be made in table. Table goes as below
-----------------------------------------------------
|_id| name | client_id | user_id | rating | count |
-----------------------------------------------------
|1 | Four | 1 | 1 | 4 | 1 |
|2 | three | 1 | 1 | 3 | 2 |
|3 | two | 1 | 1 | 2 | 3 |
|4 | five | 1 | 1 | 5 | 4 |
|5 | two | 1 | 2 | 2 | 1 |
|6 | three | 1 | 2 | 3 | 2 |
|7 | two | 2 | 1 | 2 | 1 |
|8 | three | 2 | 1 | 3 | 2 |
-----------------------------------------------------
For rating of client_id 1 I want out put like
-----------------------------------------------------
|_id| name | client_id | user_id | rating | count |
-----------------------------------------------------
|4 | five | 1 | 1 | 5 | 4 |
|6 | three | 1 | 2 | 3 | 2 |
-----------------------------------------------------
so far I tried SELECT * FROM test
where client_id = 1 group by client_id order by count desc;
but not getting expected result, any help??
You can use left join on the same table as
select t1.* from test t1
left join test t2 on t1.user_id = t2.user_id
and t1.client_id = t2.client_id
and t1._id < t2._id
where
t2._id is null
and t1.client_id = 1
order by t1.`count` desc;
Using un-correlated subquery you may do as
select t1.* from test t1
join (
select max(_id) as _id,
client_id,
user_id
from test
where client_id = 1
group by client_id,user_id
)t2
on t1._id = t2._id
and t1.client_id = t2.client_id
order by t1.`count` desc;
UPDATE : From the comment how to join another table into above , for this here is an example
mysql> select * from users ;
+------+------+
| _id | name |
+------+------+
| 1 | AAA |
| 2 | BBB |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from test ;
+------+-------+-----------+---------+--------+-------+
| _id | name | client_id | user_id | rating | count |
+------+-------+-----------+---------+--------+-------+
| 1 | four | 1 | 1 | 4 | 1 |
| 2 | three | 1 | 1 | 3 | 2 |
| 3 | two | 1 | 1 | 2 | 3 |
| 4 | five | 1 | 1 | 5 | 4 |
| 5 | two | 1 | 2 | 2 | 1 |
| 6 | three | 1 | 2 | 3 | 2 |
| 7 | two | 2 | 1 | 2 | 1 |
| 8 | three | 2 | 1 | 3 | 2 |
+------+-------+-----------+---------+--------+-------+
select t1.*,u.name from test t1
join users u on u._id = t1.user_id
left join test t2 on t1.user_id = t2.user_id
and t1.client_id = t2.client_id
and t1._id < t2._id
where
t2._id is null
and t1.client_id = 1
order by t1.`count` desc;
Will give you
+------+-------+-----------+---------+--------+-------+------+
| _id | name | client_id | user_id | rating | count | name |
+------+-------+-----------+---------+--------+-------+------+
| 4 | five | 1 | 1 | 5 | 4 | AAA |
| 6 | three | 1 | 2 | 3 | 2 | BBB |
+------+-------+-----------+---------+--------+-------+------+
Note that the join to users table is inner join and this will require all the user to be preset in users table which are in test table
If some users are missing in the users table then use left join this will have null values for the data selected from users table.
You may try something like
select _id, name, client_id, user_id, rating, max(count)
from clients
group by client_id
Try it
SELECT * FROM test
where client_id = 1
group by user_id
order by count desc