I have 4 tables:
comments
+----+-----------+--------------+-------+
| id | content | user_id | article_id |
+----+-----------+--------------+-------+
| 1 | Comment 1 | 2 | 5 |
| 2 | Comment 2 | 5 | 3 |
| 3 | Comment 3 | 1 | 6 |
| 4 | Comment 4 | 6 | 8 |
| 5 | Comment 5 | 1 | 6 |
| ...| ... | ... | ... |
+----------------+---------+------------+
votes
+----+----------+--------------+---+
| id | type | user_id | article_id |
+----+----------+--------------+---+
| 1 | 1 | 2 | 5 |
| 2 | 1 | 3 | 3 |
| 3 | 0 | 1 | 6 |
| 4 | 1 | 7 | 4 |
| 5 | 0 | 9 | 4 |
| 6 | 0 | 1 | 6 |
| ...| ... | ... | ... |
+------------+----------+----------+
notifications (object_id is the id of the vote|comment)
+----+----------+--------------+-------------+-------------+--------------+
| id | object_url| object_id |activitytype_id| sender_id | recipient_id |
+----+----------+------------+---------------+-------------+--------------+
| 1 | /../../.. | 1 | 2 | 2 | 6 |
| 2 | /../../.. | 2 | 2 | 3 | 2 |
| 3 | /../../.. | 1 | 1 | 2 | 7 |
| 3 | /../../.. | 2 | 1 | 5 | 2 |
| 3 | /../../.. | 3 | 1 | 1 | 3 |
| 3 | /../../.. | 3 | 3 | 1 | 2 |
| 3 | /../../.. | 4 | 2 | 7 | 8 |
| 3 | /../../.. | 5 | 3 | 9 | 1 |
| 3 | /../../.. | 6 | 3 | 1 | 5 |
| ...| ... | ... | ... | | |
+----+-----------+-----------+---------------+-------------+--------------+
activitytypes
+----+------------+
| id | label |
+----+------------+
| 1 | comment |
| 2 | vote up |
| 3 | vote down |
| ...| ... |
+-----------------+
I would like to get notifications like on stackoverflow.
I want to query the last notification (with comment content if the activity type is a comment or null if not) for every activitytype and object_url combinaison for a specific user.
For example I have 3 artiles A,B and C which all have 3 comments, 4 voteup and 2 votedown. How to get the last comment, voteup and votedown for every article ?
I have tried this query:
SELECT n.id, n.object_url , n.object_id, n.activitytype_id, IF(n.activitytypeId = 1,
(SELECT content FROM comments WHERE id=n.object_id), null) AS activitycontent
FROM notifications n WHERE n.recipient_id =1
GROUP BY n.activitytype_id,n.object_url
ORDER BY n.id DESC
But it doesn't work. Can anyone help ?
EDIT:
This following query in farhadamjady's answer gives me the first comment:
SELECT
n.id,
n.object_url,
n.object_id,
n.activitytype_id,
cm.content AS activitycontent
FROM
notifications n
LEFT OUTER JOIN `COMMENT` AS cm ON cm.id = n.object_id and n.activitytypeId = 1
WHERE
n.recipient_id = 1
GROUP BY
n.activitytype_id,
n.object_url
HAVING MAX(cm.id)
ORDER BY
n.id DESC
How can I change it to get the last ?
you should use left outer join like this :
SELECT
n.id,
n.object_url,
n.object_id,
n.activitytype_id,
cm.content AS activitycontent
FROM
notifications n
LEFT OUTER JOIN `COMMENT` AS cm ON cm.id = n.object_id and n.activitytypeId = 1
WHERE
n.recipient_id = 1
GROUP BY
n.activitytype_id,
n.object_url
HAVING MAX(cm.id)
ORDER BY
n.id DESC
Related
How can I write a Query to join 3 tables, resulting a ordered and sorted list?
I have 3 tables with the following structure
:
Table Users:
|---------------------------|
| Users |
|---------------------------|
| ID | Name |
|-------------|-------------|
| 1 | John |
|-------------|-------------|
| 2 | David |
|-------------|-------------|
| 3 | James |
|-------------|-------------|
| 4 | Jack |
|-------------|-------------|
Table Questions:
|-------------------------------------------------------|
| Questions |
|-------------------------------------------------------|
| ID | Question |
|-------|-----------------------------------------------|
| 1 | How old are you working in this company? |
|-------|-----------------------------------------------|
| 2 | How many customers do you notice? |
|-------|-----------------------------------------------|
| 3 | What is your salary? |
|-------|-----------------------------------------------|
| 4 | Do you speak another language? |
|-------|-----------------------------------------------|
Table Replies
|----------------------------------------|
| Replies |
|----------------------------------------|
| ID | USER ID | QUESTION ID | Reply |
|-----|---------|-------------|----------|
| 1 | 1 | 1 | 10 |
|-----|---------|-------------|----------|
| 2 | 1 | 2 | 30 |
|-----|---------|-------------|----------|
| 3 | 1 | 3 | 3000 |
|-----|---------|-------------|----------|
| 4 | 1 | 4 | yes |
|-----|---------|-------------|----------|
| 5 | 2 | 1 | 7 |
|-----|---------|-------------|----------|
| 6 | 2 | 2 | 25 |
|-----|---------|-------------|----------|
| 7 | 2 | 3 | 1500 |
|-----|---------|-------------|----------|
| 8 | 2 | 4 | no |
|-----|---------|-------------|----------|
| 9 | 3 | 1 | 5 |
|-----|---------|-------------|----------|
| 10 | 3 | 2 | 50 |
|-----|---------|-------------|----------|
| 11 | 3 | 3 | 2000 |
|-----|---------|-------------|----------|
| 12 | 3 | 4 | yes |
|-----|---------|-------------|----------|
| 13 | 4 | 1 | 7 |
|-----|---------|-------------|----------|
| 14 | 4 | 2 | 40 |
|-----|---------|-------------|----------|
| 15 | 4 | 3 | 2000 |
|-----|---------|-------------|----------|
| 16 | 4 | 4 | yes |
|-----|---------|-------------|----------|
I need to write a SQL Query to filter and sort these results.
Almost like an Excel.
Example:
I need to select who speaks another language, who serves from 5 to 100 clients, ordering for the decreasing salary and years in the descending company.
It should result like this:
|--------------------------------------------------------------------|
| Result |
|--------------------------------------------------------------------|
| ORDER | NAME | QUESTION 1 | QUESTION 2 | QUESTION 3 | QUESTION 4 |
|-------|--------|------------|------------|------------|------------|
| 1 | John | 10 | 30 | 3000 | Yes |
|-------|--------|------------|------------|------------|------------|
| 2 | Jack | 7 | 40 | 2000 | Yes |
|-------|--------|------------|------------|------------|------------|
| 3 | James | 5 | 50 | 2000 | Yes |
|-------|--------|------------|------------|------------|------------|
Any suggestions?
Thanks
Do the JOIN with conditional aggregation :
select u.user_id, u.name,
max(case when r.QUESTIONID = 1 then r.reply) as QUESTION1,
max(case when r.QUESTIONID = 2 then r.reply) as QUESTION2,
max(case when r.QUESTIONID = 3 then r.reply) as QUESTION3,
max(case when r.QUESTIONID = 4 then r.reply) as QUESTION4
from Replies r inner join
Users u
on u.user_id = r.user_id
group by u.user_id, u.name;
EDIT :
select t.*
from ( <query> ) t
where . . .;
I'm migrating a database from one application to another. In the first one I've two tables: proyectos and presupuestos. A row in 'proyectos' can have one or more rows in 'presupuestos'.
The new application has a field in presupuestos that is made concatenating the code of the proyect with the number of 'presupuesto' of this proyect. That's what I don't know how to do it.
My tables are like:
Proyectos:
+--------------+------------------+
| proyectos_id | proyectos_codigo |
+--------------+------------------+
| 1 | E+-00001 |
| 2 | E+-00002 |
| 3 | E+-00003 |
| 4 | E+-00004 |
| 5 | E+-00005 |
+--------------+------------------+
Presupuestos:
+-----------------+--------------+
| presupuestos_id | proyectos_id |
+-----------------+--------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 3 |
| 6 | 3 |
| 7 | 3 |
| 8 | 4 |
| 9 | 4 |
| 10 | 5 |
+-----------------+--------------+
I've tried with this query:
select presupuestos_id, p.proyectos_id, concat(pr.proyectos_codigo,'_1') from presupuestos p join proyectos pr on p.proyectos_id = pr.proyectos_id
Which result is:
+-----------------+--------------+----------------------------------+
| presupuestos_id | proyectos_id | concat(pr.proyectos_codigo,'_1') |
+-----------------+--------------+----------------------------------+
| 1 | 1 | E+-00001_1 |
| 2 | 1 | E+-00001_1 |
| 3 | 1 | E+-00001_1 |
| 4 | 2 | E+-00002_1 |
| 5 | 3 | E+-00003_1 |
| 6 | 3 | E+-00003_1 |
| 7 | 3 | E+-00003_1 |
| 8 | 4 | E+-00004_1 |
| 9 | 4 | E+-00004_1 |
| 10 | 5 | E+-00005_1 |
+-----------------+--------------+----------------------------------+
But obviusly, It doesn't what I want. My desired result is:
+-----------------+--------------+----------------------------------+
| presupuestos_id | proyectos_id | some code |
+-----------------+--------------+----------------------------------+
| 1 | 1 | E+-00001_1 |
| 2 | 1 | E+-00001_2 |
| 3 | 1 | E+-00001_3 |
| 4 | 2 | E+-00002_1 |
| 5 | 3 | E+-00003_1 |
| 6 | 3 | E+-00003_2 |
| 7 | 3 | E+-00003_3 |
| 8 | 4 | E+-00004_1 |
| 9 | 4 | E+-00004_2 |
| 10 | 5 | E+-00005_1 |
+-----------------+--------------+----------------------------------+
Try this :
SELECT presupuestos_id, p.proyectos_id,
CONCAT(pr.proyectos_codigo,'_',
(CASE p.proyectos_id
WHEN #p_id
THEN #rownumber := #rownumber + 1
ELSE #rownumber := 1 AND #p_id := p.proyectos_id END)
)AS result
FROM presupuestos p
JOIN proyectos pr ON p.proyectos_id = pr.proyectos_id
JOIN (SELECT #rownumber:=0, #p_id:='') AS t
This should do what you want, although the answer by RubahMalam looks better... :
SELECT a.presupuestos_id, a.proyectos_id, concat(p.proyectos_codigo,'_', count(*)) as "Some code"
FROM (
SELECT pr.presupuestos_id, pr.proyectos_id
FROM Presupuestos pr JOIN Proyectos p ON pr.proyectos_id = p.proyectos_id
) a
JOIN (
SELECT pr.presupuestos_id, pr.proyectos_id
FROM Presupuestos pr JOIN Proyectos p ON pr.proyectos_id = p.proyectos_id
) b
ON a.proyectos_id = b.proyectos_id AND a.presupuestos_id >= b.presupuestos_id
JOIN Proyectos p ON a.proyectos_id = p.proyectos_id
GROUP BY a.proyectos_id, a.presupuestos_id, p.proyectos_codigo
Sample SQL Fiddle
I've tried the following queries but unfortunately they don't work :(.
Worth mentioning that each customer has more than one CustomerUsers
select (a.TotalJobs / b.DaysActive) from
(select count(jr.id) as TotalJobs
from jobrequests jr, customers c, customerusers cu
where jr.customeruserid=cu.id
and cu.customerid=c.id
group by c.name) as a,
(select datediff(curdate(), from_unixtime(c.CreationTime)) as DaysActive
from customers c
group by c.name) as b
Please see below the tables
Jobs:
+----+--------------+
| ID | JobRequestID |
+----+--------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
| 8 | 3 |
| 9 | 3 |
| 10 | 3 |
| 11 | 4 |
| 12 | 4 |
| 13 | 5 |
| 14 | 5 |
| 15 | 6 |
| 16 | 7 |
| 17 | 8 |
| 18 | 8 |
| 19 | 9 |
| 20 | 10 |
+----+--------------+
JobRequests:
+----+---------------+
| ID | CustomeUserID |
+----+---------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 4 |
| 8 | 4 |
| 9 | 4 |
| 10 | 5 |
| 11 | 5 |
| 12 | 5 |
| 13 | 6 |
| 14 | 6 |
| 15 | 7 |
+----+---------------+
CustomerUsers:
+----+------------+
| ID | CustomerID |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 3 |
| 9 | 3 |
| 10 | 4 |
+----+------------+
Customers:
+----+------+--------------+
| ID | Name | CreationTime |
+----+------+--------------+
| 1 | a | 1415814194 |
| 2 | b | 1415814194 |
| 3 | c | 1415986994 |
| 4 | d | 1415986994 |
+----+------+--------------+
For the moment it returns 16 results (4X4), dividing each result from 1st sub-query to each result from the 2nd one (each of these sub-queries return 4 results). Can anyone please help me to get this to divide only 1 result from sub-query 1 to it's corespondent from sub-query 2?
Thank you in advance.
I suspect that you can do what you want this a query like this:
select c.name, count(*) / (datediff(curdate(), from_unixtime(c.CreationTime))
from customerusers cu join
jobrequests jr
on jr.customeruserid = cu.id join
customers c
on cu.customerid = c.id
group by c.name;
I don't see why you need two subqueries for this.
I'm guessing you need to join your results together -- as currently written, you're producing a cartesian product.
Try something like this adding c.id to each subquery (it's better to group by it presumably rather than the name):
select (a.TotalJobs / b.DaysActive)
from (
select c.id,
count(jr.id) as TotalJobs
from jobrequests jr
join customers c on jr.customeruserid=cu.id
join customerusers cu on cu.customerid=c.id
group by c.id) a join (
select c.id,
datediff(curdate(), from_unixtime(c.CreationTime)) as DaysActive
from customers c
group by c.id) b on a.id = b.id
Please note, I've updated your syntax to use the more standard join syntax.
I have a complicated ordering issue in my query.
Raw, Unordered Data:
+------+--------+-----------+
| id | job_id | action_id |
+------+--------+-----------+
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 1 |
| 4 | 2 | 3 |
| 5 | 4 | 1 |
| 6 | 1 | 2 |
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| 9 | 4 | 2 |
+------+--------+-----------+
Required Ordering:
+------+--------+-----------+
| id | job_id | action_id |
+------+--------+-----------+
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| | | | * blank lines added for clarity,
| 5 | 4 | 1 | not desired in actual data
| 9 | 4 | 2 |
| | | |
| 3 | 1 | 1 |
| 6 | 1 | 2 |
| | | |
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 4 | 2 | 3 |
+------+--------+-----------+
The theory behind this ordering:
the largest id is the most recently added entry
the most recent id with action_id of 1
followed by the entries with ascending action_ids that have the same job_id
then the next most recent action_id of 1
ad infinitum
EDIT: I'm not able to add columns to the table in order to aid in sorting, as I've seen in some other solutions to ordering questions.
Any help is greatly appreciated!
My best shot is this:
SELECT * FROM tbl
ORDER BY FIND_IN_SET(job_id,
(SELECT GROUP_CONCAT(job_id ORDER BY ID DESC)
FROM tbl WHERE action_id = 1));
I didn't find a way to do it easily, What do you think of the following code :
select c.id, c.job_id, c.action_id
from (select a.id, a.job_id, a.action_id, min(b.id) as related_id
from myTable a
inner join myTable b
on a.job_id=b.job_id
group by a.job_id) c
group by c.id
order by c.related_id desc, c.action_id
Hi I've been searching for this, and I've found solutions for other sample code, but I can't figure out how to implement for mine.
SELECT `gameDBGames`, `game_id`, MIN(`gamePrice`) AS `gamePrice`
FROM `games`
LEFT JOIN `platforms` ON `gamePlatform` = `platform_id`
LEFT JOIN `bundles` ON `gameBundle` = `bundle_id`
LEFT JOIN `currency` ON `bundleCurrency` = `currency_id`
WHERE `bundleEnd` > CURDATE() AND `bundleType` = "1" AND `gameDBGames` != "0"
GROUP BY `gameDBGames`
Here is my actual query. This returns the minimum price, but do not correspond to the game_id. How could I do that? I believe is doing a inner join like this:
SELECT `gameDBGames`, `game_id`, MIN(`gamePrice`) AS `gamePrice`
FROM `games`
LEFT JOIN `platforms` ON `gamePlatform` = `platform_id`
LEFT JOIN `bundles` ON `gameBundle` = `bundle_id`
INNER JOIN (....)
LEFT JOIN `currency` ON `bundleCurrency` = `currency_id`
WHERE b.`bundleEnd` > CURRDATE() AND b.`bundleType` = "1" AND g.`gameDBGames` != "0"
Thank you.
EDIT: Sorry I don't know what I was thinking not posting the table structure.
The game_id is the unique id (e.g. same game but with different prices) , and gameDBGames is an ID for a game (e.g. gameDBGames = 1, can have price 40 or 30) that's why I'm grouping by gameDBGames. An the aim is getting a unique gameDBGames with the minimum price.
What I have.
+--------------+---------+----+---------+
| table games | | | |
+--------------+---------+----+---------+
| game_id | int | AI | PRIMARY |
| gameDBGames | int | | |
| gamePrice | float | | |
| gamePlatform | tinyint | | |
| gameBundle | int | | |
+--------------+---------+----+---------+
+---------+-------------+-----------+--------------+------------+
| game_id | gameDBGames | gamePrice | gamePlatform | gameBundle |
+---------+-------------+-----------+--------------+------------+
| 1 | 1 | 20 | 1 | 1 |
| 2 | 2 | 20 | 2 | 1 |
| 3 | 2 | 15 | 2 | 1 |
| 4 | 3 | 17 | 1 | 1 |
| 5 | 3 | 20 | 1 | 1 |
| 6 | 3 | 15 | 1 | 1 |
| 7 | 4 | 16 | 2 | 2 |
| 8 | 5 | 18 | 2 | 2 |
| 9 | 5 | 14 | 2 | 2 |
| 10 | 6 | 15 | 1 | 2 |
+---------+-------------+-----------+--------------+------------+
What I get.
+---------+-------------+-----------+--------------+------------+
| game_id | gameDBGames | gamePrice | gamePlatform | gameBundle |
+---------+-------------+-----------+--------------+------------+
| 1 | 1 | 20 | 1 | 1 |
| 2 | 2 | 15 | 2 | 1 |
| 4 | 3 | 15 | 1 | 1 |
| 7 | 4 | 16 | 2 | 2 |
| 8 | 5 | 14 | 2 | 2 |
| 10 | 6 | 15 | 1 | 2 |
+---------+-------------+-----------+--------------+------------+
As you can see the game_id do not correspond to the gamePrice. It should be like this.
+---------+-------------+-----------+--------------+------------+
| game_id | gameDBGames | gamePrice | gamePlatform | gameBundle |
+---------+-------------+-----------+--------------+------------+
| 1 | 1 | 20 | 1 | 1 |
| 3 | 2 | 15 | 2 | 1 |
| 6 | 3 | 15 | 1 | 1 |
| 7 | 4 | 16 | 2 | 2 |
| 9 | 5 | 14 | 2 | 2 |
| 10 | 6 | 15 | 1 | 2 |
+---------+-------------+-----------+--------------+------------+
Hope you understand now, sorry for not been more explained before. If you need something else please ask. Thank you.
EDIT 2 (Mark)
The solution put by Mark gives me this errors.
There are gameDBGames repeated.
Some games are non shown.
I've updated the WHERE clause just in case is needed.
Try:
SELECT g.`gameDBGames`, g.`game_id`, g.`gamePrice`
FROM (SELECT `gameDBGames`, MIN(`gamePrice`) AS `minPrice`
FROM `games`
GROUP BY `gameDBGames`) mn
JOIN `games` g
ON mn.`gameDBGames`=g.`gameDBGames` and mn.`minPrice`=g.`gamePrice`
LEFT JOIN `platforms` p ON g.`gamePlatform` = p.`platform_id`
LEFT JOIN `bundles` b ON g.`gameBundle` = b.`bundle_id`
LEFT JOIN `currency` c ON b.`bundleCurrency` = c.`currency_id`
WHERE ....
The GROUP BY in the main query should not be required, due to the grouping in the subquery - however, more than one game_id will be returned for a given gameDBGames if there is more than one game_id with the same minimum price for a given gameDBGames.