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
Related
I have a table in which i have four columns
id
user_type
user_role
org_id
Now if the user_type is 1 (i.e individual) then the user_role and org_id will be null,
but if the user_type is 2 (i.e organisation) then it will have org_id (id of the organisation) and user_role (his role in the organisation).
There are three type of role in an organisation
admin (only one person could be the admin).
finance secrearty (could be more than one).
approvers (also could be more than one).
What I want
I want to fetch only one person (probably admin of the organisation) if the user_type is 2 from each organisation and all the users having user_type 1.
My table
-------------------------------------
| tbl_user |
-------------------------------------
| id | user_Type | user_role | org_id|
--------------------------------------
| 1 | 1 | null | null |
| 2 | 2 | 1 | 1 |
| 3 | 2 | 2 | 1 |
| 4 | 2 | 3 | 1 |
| 5 | 2 | 3 | 1 |
| 6 | 1 | null | null |
| 7 | 2 | 1 | 2 |
| 8 | 2 | 2 | 2 |
| 9 | 2 | 3 | 2 |
| 10 | 2 | 3 | 2 |
Expected result
-------------------------------------
| tbl_user |
-------------------------------------
| id | user_Type | user_role | org_id|
--------------------------------------
| 1 | 1 | null | null |
| 2 | 2 | 1 | 1 |
| 7 | 2 | 1 | 2 |
| 6 | 1 | null | null |
Thanks.
Is this what you need?
SELECT * FROM YourTable t
WHERE t.user_type = 1
OR t.user_role = 1
This will give you the expected result but will also include ID = 6
SELECT *
FROM tbl_user
WHERE (user_Type = 1 AND user_role IS NULL)
OR (user_Type = 2 AND user_role = 1)
You can avoid it to be shown by placing a condition AND id != 6, there are no other condition that can avoid it to be returned
SELECT *
FROM tbl_user
WHERE ((user_Type = 1 AND user_role IS NULL)
OR (user_Type = 2 AND user_role = 1))
AND id != 6
Here you can look at live fiddle
select t1.*
from table t1
inner join (
select min(id)
from table
group by org_id
) t2
on (t1.id = t2.id
and t1.user_type = 2)
or t1.user_type = 1;
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;
I am trying to create a query with a GROUP_CONCAT added as a new column in my current query, first here are my tables:
Users table
+----+----------+--------------+
| id | username | date_created |
+----+----------+--------------+
| 1 | user1 | 2000-03-16 |
| 2 | user2 | 2001-05-14 |
| 3 | user3 | 2002-01-13 |
| 4 | user4 | 2003-03-14 |
+----+----------+--------------+
Shifts table
+----+------------+--------------+
| id | shift_name | date_created |
+----+------------+--------------+
| 1 | shift1 | 2002-05-10 |
| 2 | shift2 | 2002-07-11 |
| 3 | shift3 | 2002-09-23 |
+----+------------+--------------+
Accounts table
+----+--------------+--------------+
| id | account_name | date_created |
+----+--------------+--------------+
| 1 | account1 | 2001-05-01 |
| 2 | account2 | 2001-05-02 |
| 3 | account3 | 2001-05-03 |
+----+--------------+--------------+
Shift Mapping table
+----+---------+----------+------------+
| id | user_id | shift_id | account_id |
+----+---------+----------+------------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 3 | 3 | 1 | 1 |
+----+---------+----------+------------+
basically, I want a query that gets all the user (to display in a table) with a custom column that shows all shift that is attach to that user(if there is no shift attach to that obviously has a null result)
Here is the query I've done so far:
SELECT users.id AS user_id, users.username, users.date_created,
GROUP_CONCAT(DISTINCT (t.shift_name)) AS shifts
FROM (`users`)
LEFT JOIN
(SELECT s.shift_name, sm.user_id FROM shift_map sm
LEFT JOIN shifts s ON sm.shift_id = s.id) t
ON users.id = t.user_id
GROUP BY user_id
ORDER BY `users`.`date_created` DESC;
Now there is no problem getting the users with a shift attach to it, my problem is that the users with no shifts attach to only returns 1 result which is caused by the GROUP BY user_id how can I exclude the users with no shift in the GROUP BY or how can I return all the users with attached shifts and with no attach shifts? Thanks.
Update
Here is the example result I want to see:
+---------+----------+--------------+----------------+
| user_id | username | date_created | shifts |
+---------+----------+--------------+----------------+
| 1 | user1 | 2000-03-16 | shift1,shift2 |
| 2 | user2 | 2001-05-14 | (NULL) |
| 3 | user3 | 2002-01-13 | shift1 |
| 4 | user4 | 2003-03-14 | (NULL) |
+---------+----------+--------------+----------------+
My problem in my query is that it only shows only 1 user with null shifts.
SELECT users.id AS user_id, users.username, users.date_created,
GROUP_CONCAT(DISTINCT (t.shift_name)) AS shifts
FROM (`users`)
LEFT JOIN
(SELECT s.shift_name, sm.user_id FROM mapping sm
LEFT JOIN shifts s ON sm.shift_id = s.id) t
ON users.id = t.user_id
GROUP BY username
ORDER BY user_id
+---------+----------+--------------+---------------+
| user_id | username | date_created | shifts |
+---------+----------+--------------+---------------+
| 1 | user1 | 2000-03-16 | shift1,shift2 |
| 2 | user2 | 2001-03-16 | NULL |
| 3 | user3 | 2002-03-16 | shift1 |
| 4 | user4 | 2003-03-16 | NULL |
+---------+----------+--------------+---------------+
My Bad, I can just use the simple LEFT JOIN:
SELECT users.id AS user_id, users.username, users.date_created,
GROUP_CONCAT(DISTINCT (shift.shift_name)) AS shifts
FROM (`users`)
LEFT JOIN shifts_map ON users.id = shifts_map.user_id
LEFT JOIN shifts ON shifts_map.shift_id = shift.id
GROUP BY user_id
ORDER BY `users`.`date_created` DESC;
I am just complicating the query, Forgot that the simple LEFT JOIN can do the trick. Thanks.
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
I have 3 table
major table:
+----+------------+
| id | major |
+----+------------+
| 1 | Computer |
| 2 | Architect |
| 3 | Designer |
+----+------------+
classroom table:
+----+----------+-------+
| id | major_id | name |
+----+----------+-------+
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 1 | C |
| 4 | 2 | A |
| 5 | 2 | B |
| 6 | 3 | A |
+----+----------+-------+
and finally, student_classroom table
+----+------------+--------------+----------+
| id | student | classroom_id | status |
+----+------------+--------------+----------+
| 1 | John | 1 | Inactive |
| 2 | Defou | 2 | Active |
| 3 | John | 2 | Active |
| 4 | Alexa | 1 | Active |
| 5 | Nina | 1 | Active |
+----+------------+--------------+----------+
how can I use propel to build query below
select
a.id,
a.major,
b.number_of_student,
c.number_of_classroom
from major a
left join (
select
major.major_id,
count(student_classroom.id) as number_of_student
from major
left join classroom on classroom.major_id = major.id
left join student_classroom on student_classroom.classroom_id = classroom.id
where student_classroom.`status` = 'Active'
group by major_id
) b on b.major_id = a.major_id
left join (
select
major.major_id,
count(classroom.id) as number_of_classroom
from major
left join classroom on classroom.major_id = major.id
group by major_id
) c on c.major_id = a.major_id
Because I want the final result would be something like this, I spend hours trying to figure it out without success.
+----+------------+-------------------+---------------------+
| id | major | number_of_student | number_of_classroom |
+----+------------+-------------------+---------------------+
| 1 | Computer | 4 | 3 |
| 2 | Architect | 0 | 2 |
| 3 | Designer | 0 | 1 |
+----+------------+-------------------+---------------------+
Try this
select
m.id,
m.major,
count(distinct s.id) as number_of_student ,
count(distinct c.id) as number_of_classroom
from major m
left join classroom c on
(m.id = c.major_id)
left join student_classroom s
on (s.classroom_id = c.id and c.major_id = m.id and s.status = 'active')
group by m.id
order by m.id