I'm currently trying to write a query which involves 5 main tables, 2 of which are referring to a 3rd with foreign keys, but not relating to each-other... and one of the first 2 tables is the main subject of the query. Here's a basic synopsis.
instance user
-------- ----
id id
name name
user_id
def def_map
--- ------
id id
name instance_id
user_id def_id
def_data
--------
id
name
def_id
user_id
What I want to do is get a list of all of the 'def_map's for a single user. In each row I'd like the associated def_data to be displayed as well. So the rows would be like:
instance.id, def.id, def.name, def_data.name, user.id
I can figure out how to get all info except def_data.name in the result, or all info except for instance.id ... but can't figure out how to get then all together using one query. Is it possible? I think part of the problem is I don't know if there is a special word that describes this type of query so I would know what to read up on.
I'm using DQL, but examples in SQL would be just as useful. Thanks in advance for any help.
If you can pull the data individually using 2 queries you simply need to UNION them together
SELECT user.id, i.id, d.id, dd.name
FROM user u
INNER JOIN instance i ON u.id=i.user_id
INNER JOIN def d ON dm.user_id = u.id
INNER JOIN def_data dd ON dd.def_id = d.id
UNION ALL
SELECT u.id, i.id AS instance_id, d.id, dd.name
FROM instance i
INNER JOIN user u ON u.id=i.user_id
INNER JOIN defmap dm ON dm.instance_id=i.id
INNER JOIN def_data dd ON dd.def_id=dm.def_id
select I.id, D.id, D.name, DD.name, U.id
from user U inner join instance I on I.user_id = U.id
Inner join def D on D.user_id = U.id
inner join def_map DM on DM.def_id = D.id AND I.id = DM.instance_id
inner join def_data DD on DD.def_id = D.id AND U.id = DD.user_id
Test data:
USER
+----+-------------------------+
| id | name |
+----+-------------------------+
| 1 | Name1 |
+----+-------------------------+
Instance
+----+------+---------+
| id | name | user_id |
+----+------+---------+
| 1 | I1 | 1 |
+----+------+---------+
def_map
+--------+-------------+--------+
| id | instance_id | def_id |
+--------+-------------+--------+
| 1 | 1 | 1 |
+--------+-------------+--------+
def
+--------------+------+
| id | name | user_id |
+--------------+------+
| 1 | df1 | 1 |
+--------------+------+
def_data
+--------+------+--------+---------+
| id | name | def_id | user_id |
+--------+------+--------+---------+
| 1 | dd1 | 1 | 1 |
+--------+------+--------+---------+
Result
+-------------+--------+----------+---------------+---------+
| instance.id | def.id | def.name | def_data.name | user.id |
+-------------+--------+----------+---------------+---------+
| 1 | 1 | df1 | dd1 | 1 |
+-------------+--------+----------+---------------+---------+
Related
This is the situation I'm dealing with:
I have 4 tables:
Users table:
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
+----+-------+
Assignment table:
+----+-----------------+
| id | assignment_name |
+----+-----------------+
| 11 | name1 |
| 12 | name2 |
| 13 | name3 |
+----+-----------------+
Submissions:
+----+---------------+---------+
| id | assignment_id | user_id |
+----+---------------+---------+
| 1 | 11 | 3 |
| 2 | 12 | 1 |
| 3 | 11 | 2 |
+----+---------------+---------+
Group_submissions
+----+----------------+---------+
| id | submission_id | user_id |
+----+----------------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 1 |
+----+----------------+---------+
The submission table has an assignment_id to tell in which assignment the submission belongs to.
Also users can submit a group submission, where the one that does the submission goes to the submissions table, while the others go to the group_submissions table. That way it will be counted as one submission instead of being 2,3...N submission based on how many people where in the group.
How can i get the users that have submitted a submission or have participated in a group submission in a given assignment?
The result should return the user or users that have are in the submissions table or in the group_submissions table based on a assignment id
The result should look something likes this:
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
+----+-------+
It should basically return the user table.
This is what i have tried so far:
This only gives me the users that aren't in the submissions table but are in the group_submission
select * from users u
right join group_submissions gs on u.id = gs.student_id
right join assignment_submissions ass on gs.submission_id = ass.id
inner join assignments a on a.id = ass.assignment_id
where a.id = number
This only gives me the one user that made the submission (in the submissions table)
select * from users u
right join assignment_submissions ass on u.id= ass.student_id
right join group_submissions gs on ass.id = gs.submission_id
inner join assignments a on a.id = ass.assignment_id
where a.id = number
What should my join strategy be here? Or maybe joins are not the right option here.
NOTE: This is a MySQL database.
You could use exists:
select u.*
from users u
where
exists (
select 1
from submissions s
where s.user_id = u.id and s.assignment_id = ?
)
or exists (
select 1
from group_submissions gs
inner join submissions s on s.id = gs.submission_id
where gs.user_id = u.id and s.assignment_id = ?
)
I am thinking a union could do it. Like this
select * from
(
select u.*, assignment_id, 'assignment_submissions' as type from users u
inner join assignment_submissions ass on u.id= ass.student_id
union
select u.*, assignment_id, 'group_submissions' from users u
inner join group_submissions gs on u.id = gs.student_id
inner join assignments a on a.id = ass.assignment_id
)a
where assignment_id = ?
Consider the following tables:
User Table
id | name | createdAt |
-----------------------|
1 | John | 2018-02-02 |
Activity Table
id | itemId | itemTable | createdAt |
-------------------------------------|
13 | 1 | User | 2018-02-02 |
14 | 142 | Client | 2018-02-02 |
I want to be able to LEFT JOIN on a table specified in the column:
SELECT b.*
FROM activity AS a
LEFT JOIN *a.tablename* AS b
ON b.id = a.itemid
for MemSQL or MySQL
You cannot do this in a SELECT query. SQL does not operate this way.
If you know there are only two tables, you can express the query like this:
SELECT u.*
FROM USER u
WHERE u.id = (SELECT 1 FROM activity a WHERE a.itemid = u.id)
UNION ALL
SELECT c.*
FROM client c
WHERE c.id = (SELECT 1 FROM activity a WHERE a.itemid = c.id);
I have a few tables that look like the below
Users
-----------------------------------------
| id | policyId | createdAt | updatedAt |
-----------------------------------------
| 1 | 1 | 2017/8/5 | 2017/8/5 |
| 2 | 1 | 2016/4/5 | 2017/8/5 |
| 3 | 2 | 2017/7/2 | 2017/8/5 |
| 4 | 2 | 2018/8/5 | 2017/8/5 |
-----------------------------------------
Policies
------------------------------------------
| id | companyId | createdAt | updatedAt |
------------------------------------------
| 1 | 1 | 2017/8/5 | 2017/8/5 |
| 2 | 2 | 2016/4/5 | 2017/8/5 |
------------------------------------------
Companies
-----------------------------------------
| id | policyId | createdAt | updatedAt |
-----------------------------------------
| 1 | 2 | 2017/8/5 | 2017/8/5 |
| 2 | 1 | 2016/4/5 | 2017/8/5 |
-----------------------------------------
I need to answer the question "What is the id of the user for each company with the oldest account. So the output should look something like this.
Output
----------------------------------
| CompanyId | UserId | CreatedAt |
----------------------------------
| 1 | 2 | 2016/4/5 |
| 2 | 3 | 2017/7/2 |
----------------------------------
What I have gotten so far looks something like this but I know it is no were near correct.
SELECT c.id, MIN(u.createdAt) FROM companies as c
JOIN policies as p on p.companyId = c.id
JOIN users as u on u.policyId = p.id
GROUP BY c.id;
This seems to let me get the oldest date for each company user but I am not sure how to correlate the users back to that date to get the user id's. I am thinking the query above might have to be a sub-query but that is about as far as my sql knowledge goes.
Any help would be appreciated.
I need to join the whole query to itself and join by comapny id and createdAt column.
see demo here: http://sqlfiddle.com/#!9/6f4ea7/22
SELECT c.id as companyID,
u.id as userID,
u.createdAt
FROM companies as c
JOIN policies as p on p.companyId = c.id
JOIN users as u on u.policyId = p.id
JOIN (SELECT c.id as companyID,
min(u.createdAt) as min_dt
FROM companies as c
JOIN policies as p on p.companyId = c.id
JOIN users as u on u.policyId = p.id
GROUP BY c.id) sub
on c.id=sub.companyID
where u.createdAt=sub.min_dt
SELECT p.company_id, u.user_id, MIN(u.createdAt) FROM policies p, user_u,
(SELECT min(createdAt) as minDate, policy_id as policyId from users
GROUP BY policy_id) as sub
WHERE p.id = u.policy_id
AND sub.minDate = u.createdAt
AND sub.policy_id = u.policyId;
It will give u the expected output But some hard coded way
SELECT c.id as companyid,u.id as userid, MIN(u.createdAt) as createdAt FROM company as c
JOIN policies as p on p.cmpid = c.id
JOIN users as u on u.policyId = p.id
GROUP BY c.id,u.id order by MIN(u.createdAt) asc limit 2;
I want to create a query for project listings that would give the number of registered applications, excluding the ones for which the user does not exist.
In this case, considering user 10 does not exist, I should have the query results as folows:
RESULTS
+----+------------+--------------+
| id | project | applications |
+----+------------+--------------+
| 1 | MyProject1 | 3 |
| 2 | MyProject2 | 0 |
| 3 | MyProject3 | 0 |
+----+------------+--------------+
TABLES
Projects
+----+------------+
| id | name |
+----+------------+
| 1 | MyProject1 |
| 2 | MyProject2 |
| 3 | MyProject3 |
+----+------------+
applications
+----+------+------------+
| id | user | project_id |
+----+------+------------+
| 1 | 3 | 1 |
| 2 | 4 | 1 |
| 3 | 5 | 1 |
| 4 | 10 | 1 |
+----+------+------------+
users
+----+---------+
| id | Name |
+----+---------+
| 1 | Smith |
| 2 | John |
| 3 | Paul |
| 4 | Chris |
| 5 | Gabriel |
+----+---------+
The below query is not excluding the non-existing users:
SELECT `projects` . * , (
SELECT COUNT( * )
FROM `applications`
WHERE `applications`.`project_id` = `projects`.`id`
AND EXISTS (
SELECT `applications`.`id`
FROM `applications` , `users`,`project`
WHERE `application`.`user` = `users`.`id` AND `applications`.`project_id` = `project`.`id`
)
) AS `applications`
FROM `projects` ORDER BY `id` DESC LIMIT 30
I think you want left join and group by:
select p.id, p.name, count(u.id)
from projects p left join
applications a
on p.id = a.project_id left join
users u
on a.user_id = u.id
group by p.id, p.name;
However, you might want to think about fixing the data. It seems like there should be foreign key relationships between applications and projects and applications and users. The ability to have an invalid user means that there is no valid foreign key relationship to users.
Your query looks overly complicated. This should do:
select
id,
name as project,
(
select count(*)
from applications a
where a.project_id = p.id
and a.user in (select id from users)
) as applications
from projects p;
Based on previous solution
select p.id, p.name, count(u.id)
from projects p left join
applications a
on p.id = a.project_id left join
users u
on a.user = u.id
where u.id is not null
group by p.id, p.name;
When you do a left join, if the search value doesn't exists, it returns null. Then filtering by excluding null users, will give you the result.
Please find a sqlfiddle to illustrate it : http://www.sqlfiddle.com/#!9/cbfec6/3
But easiest solution would be
select p.id, p.name, count(u.id)
from projects p,applications a, users u
where a.user = u.id
and p.id = a.project_id
group by p.id, p.name;
Suppose I have a cooking show:
cookingepisodes
id | date
---------------
1 | A
2 | B
3 | C
4 | D
…
This show reviews products in these categories (left) and are linked by the table to the right:
tests testitems
id | name id | episodeid | testid | name
------------ ------------------------------------
1 | cutlery 1 | 1 | 1 | Forks
2 | spices 2 | 2 | 1 | Knives
3 | 4 | 1 | Spoons
4 | 4 | 2 | Oregano
My desired output is this:
showid | testid | testname
4 | 1,2 | cutlery, spices
3 | NULL | NULL
2 | 1 | cutlery
1 | 1 | cutlery
I've tried using this query, and it works as long as I don't need to concatenate the results (when there are two tests on the same episode). Then the join will create multiple rows based on the number of
SELECT DISTINCT e.*, i.testid, t.name AS testname
FROM cookingepisodes AS e
LEFT OUTER JOIN testitems AS i ON i.episodeid = e.id
LEFT OUTER JOIN tests AS t ON i.testid = t.id
ORDER BY e.date DESC
I've also tried something like this, but I can't get it to work because of the outer block reference (e.id):
JOIN (
SELECT GROUP_CONCAT(DISTINCT testid)
FROM testitems
WHERE testitems.episodeid = e.id
) AS i
Any tips on how I can solve this without restructuring the database?
Try this one -
SELECT
ce.id showid,
GROUP_CONCAT(te.testid) testid,
GROUP_CONCAT(t.name) testname
FROM cookingepisodes ce
LEFT JOIN testitems te
ON te.episodeid = ce.id
LEFT JOIN tests t
ON t.id = te.testid
GROUP BY
ce.id DESC;