I have a database, where there are groups that have many users (1:N). Those two tables are designed to have a unique ID for every group and unique ID for every user. But now I came to a problem that I need to SELECT an ID of a group and also an ID of a user within that group (which is different from his real database ID). Is there some way to SELECT these sub-IDs in MySQL without actually changing users table structure?
What my current table structure looks like:
-----------------
| GROUPS |
=================
| ID PK AUTO_INC|
| NAME |
| DESCRIPTION |
| ...other |
| non-related |
| columns |
-----------------
------------------
| USERS |
==================
| ID PK AUTO_INC |
| NAME |
| GROUP |
| AGE |
|...other |
| non-related |
| columns |
------------------
EDIT: What I would like looks like this:
GROUPS
----------------------------------
| ID | Name | Description | ... |
==================================
| 1 | A | First one | ... |
| 2 | B | Second one | ... |
| 3 | C | Third one | ... |
----------------------------------
USERS
--------------------------------------
| ID | Name | Group | Age | ... |
======================================
| 1 | John | 1 | 35 | ... |
| 2 | Adam | 1 | 22 | ... |
| 3 | Bob | 2 | 18 | ... |
| 4 | Jane | 1 | 38 | ... |
| 5 | Emma | 2 | 56 | ... |
| 6 | Aaron | 3 | 26 | ... |
| 7 | Alice | 2 | 48 | ... |
--------------------------------------
And as a result I would like to get:
-----------------------------------
| GID | UID| Name | Age | ... |
===================================
| 1 | 1 | John | 35 | ... |
| 1 | 2 | Adam | 22 | ... |
| 1 | 3 | Jane | 38 | ... |
| 2 | 1 | Bob | 18 | ... |
| 2 | 2 | Emma | 56 | ... |
| 2 | 3 | Alice | 48 | ... |
| 3 | 1 | Aaron | 26 | ... |
-----------------------------------
I hope now it will make sense.
Thanks in advance.
The only way I know how to do this is with variables
select
t.id,
t.name,
t.group,
t.rank as newid
from (
select
u.id,
u.name,
u.group,
If(#group <> u.`Group`, #rownum := 1, #rownum := #rownum + 1) AS rank,
#group := u.`Group`
from
Users u
cross join (
select
#rownum := NULL,
#group := 0
) as r
order by
u.group,
u.id
) as t
order by
t.group,
t.id;
This method isn't guaranteed to work, but in practice it seems to.
Example SQLFiddle
Related
There are two tables,
Table A has a three-level structure that looks like
| id | name | level | up_level_id |
| :------- | :-------: | :------: | ----------:|
| 1 | lv1_name1 | 1 | null |
| 2 | lv1_name2 | 1 | null |
| 3 | lv2_name1 | 2 | 1 |
| 4 | lv2_name2 | 2 | 2 |
| 5 | lv3_name1 | 3 | 3 |
| 6 | lv3_name2 | 3 | 3 |
| 7 | lv3_name3 | 3 | 4 |
| 8 | lv3_name4 | 3 | 4 |
Table B looks like
| amount | org_id |
| -------- | -------- |
| 12,000 | 5 |
| 15,000 | 6 |
| 20,000 | 7 |
| 18,000 | 8 |
Table A and Table B can be joined on A.id=B.org_id, but they are all at the level-3 of Table A(Only level-3 has their amount)
I want to query the top-level name,level-1 name, and the summary amount that looks like
| sum_amount | top_lvl_name |
| -------- | -------- |
| 27,000 | lv1_name1 |
| 38,000 | lv1_name2 |
For Testing, I have already accomplished the query of the level-1 name from the level-3 id in TableA
The SQL is as follows
SELECT name
FROM TableA
WHERE id IN (
SELECT up_level_id
FROM Table A
WHERE id IN (
SELECT up_level_id
FROM Table A
WHERE id=5) --query the id:5's top-level name
);
But when I join these two tables as follows, it goes wrong
SELECT sum(amount) AS sum_amount, name AS top_lvl_name
FROM TableA, TableB
WHERE id = org_id
AND id IN (
SELECT up_level_id
FROM TableA
WHERE id IN (
SELECT up_level_id
FROM TableA
WHERE TableA.id IN(
SELECT org_id
FROM TABLEB
)
)
);
I get nothing as above
What can I do to make this query to be correct?
Thanks for everyone's answer and comment.
Finally, I find it very difficult to query the result as I wish. So, I've come up with a shortcut——create a new table that a three-level structure recorded horizontally, which looks like
| lv1_id | lv2_name | lv2_id | lv2_name | lv3_id | lv3_name |
| :------- | :-------: | :------: | :----------:| :------: | :----------:|
| 1 | lv1_name1 | 3 | lv2_name1 | 5 | lv3_name1 |
| 1 | lv1_name1 | 3 | lv2_name1 | 6 | lv3_name2 |
| 2 | lv1_name2 | 4 | lv2_name1 | 7 | lv3_name3 |
| 2 | lv1_name2 | 4 | lv2_name1 | 8 | lv3_name4 |
As above,I can easily connect two tables
I'm trying to run a report on User ACL's. We use MYSQL and my we're prohibited from using subqueries for performance reasons. The goal is to turn this:
--------------------------------
| userName | folderID | roleID |
--------------------------------
| gronk | 1 | 1 |
| gronk | 2 | 2 |
| gronk | 4 | 2 |
| tbrady | 1 | 2 |
| jedelman | 1 | 1 |
| jedelman | 2 | 1 |
| mbutler | 1 | 2 |
| mbutler | 2 | 2 |
| bill | 1 | 3 |
| bill | 2 | 3 |
| bill | 3 | 3 |
| bill | 4 | 3 |
--------------------------------
Into this:
------------------------
| Lowest Role | Number |
------------------------
| 1 | 2 |
| 2 | 2 |
| 3 | 1 |
------------------------
I can see how to do it with a subquery. The inner query would do a group by on userName with a min(roleID). Then the outer query would do a group by on the lowest role and count(*). But I can't see how to do it without a subquery.
Also, if it helps I created a SQL Fiddle that has the data above.
I found a solution using a left join:
select UFM.roleID, count(distinct UFM.userName)
from UserFolderMembership UFM
left join UserFolderMembership UFM2 on
UFM.userName = UFM2.userName and
UFM.roleID > UFM2.roleID
where
UFM2.userName is null
group by
UFM.roleID
I have these 3 tables like that:
lecturers:
+-------------+---------+
| id-lecturer | name |
+-------------+---------+
| 1 | Johnson |
| 2 | Smith |
| ... | ... |
| ... | ... |
+-------------+---------+
subjects:
+------------+---------+
| id-subject | name |
+------------+---------+
| 1 | Math |
| 2 | Physics |
| ... | ... |
| ... | ... |
+------------+---------+
exams:
+---------+-------------+------------+------------+
| id-exam | id-lecturer | id-subject | date |
+---------+-------------+------------+------------+
| 1 | 5 | 1 | 1990-05-05 |
| 2 | 7 | 1 | ... |
| 3 | 5 | 3 | ... |
| ... | ... | ... | ... |
+---------+-------------+------------+------------+
When i try to do the first SELECT:
SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM exams e
JOIN subjects s ON e.`id-subject`=s.`id-subject`
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`
GROUP BY e.`id-lecturer`, e.`id-subject`
I get the right answer. It shows something like that:
+-------------+------------+-----------+
| id-lecturer | id-subject | exams-num |
+-------------+------------+-----------+
| 0001 | 1 | 4 |
| 0001 | 3 | 1 |
| 0001 | 4 | 1 |
| 0001 | 5 | 1 |
| 0002 | 1 | 2 |
| 0002 | 2 | 1 |
| 0002 | 4 | 1 |
| 0002 | 6 | 3 |
+-------------+------------+-----------+
Now i want to show only the max number for every lecturer, my code is:
SELECT it.`id-lecturer`, it.`id-subject`, MAX(it.`exams-num`) AS `exams-number`
FROM (
SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM egzaminy e
JOIN subjects s ON e.`id-subject`=s.`id-subject`
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`
GROUP BY e.`id-lecturer`, e.`id-subject`) it
GROUP BY it.`id-lecturer`
output:
+-------------+------------+--------------+
| id-lecturer | id-subject | exams-number |
+-------------+------------+--------------+
| 0001 | 1 | 4 |
| 0002 | 1 | 3 |
| 0003 | 1 | 2 |
| 0004 | 1 | 5 |
| 0005 | 2 | 1 |
+-------------+------------+--------------+
I get the correct numbers of the max values for each lecturer, but the subjects id doesn't match, it always takes the first row's id. How can I make it to match correctly these two fields in every row?
I guess you can simply use the same query for further conditions like below.
Select t.Lecturer_id,max(t.exams-num) from
(SELECT e.id-lecturer as Lecturer_id, e.id-subject as Subject_id,
COUNT(e.id-lecturer) AS exams-num
FROM exams e
JOIN subjects s ON e.id-subject=s.id-subject
JOIN lecturers l ON e.id-lecturer=l.id-lecturer
GROUP BY e.id-lecturer, e.id-subject ) as t
group by t.Lecturer_id
I have five tables.
Users
+--------+----------+---------------+
| UserID | Username | Password |
+--------+----------+---------------+
| 1 | Praveen | Praveen |
+--------+----------+---------------+
| 2 | Stack | StackOverflow |
+--------+----------+---------------+
| 3 | CrazyGuy | OhMyGawd! |
+--------+----------+---------------+
Messages
+-----------+-------------+-----------+----------------------------------------------+
| MessageID | MessageFrom | MessageTo | MessageContent |
+-----------+-------------+-----------+----------------------------------------------+
| 1 | 1 | 2 | Hi Stack! Praveen here! :) |
+-----------+-------------+-----------+----------------------------------------------+
| 2 | 1 | 3 | Hey Crazy Guy, you are spamming me!!! |
+-----------+-------------+-----------+----------------------------------------------+
| 3 | 2 | 3 | Hey, is Praveen speaking to you about spams? |
+-----------+-------------+-----------+----------------------------------------------+
Comments
+-----------+--------+----------------------------------------+
| CommentID | UserID | CommentContent |
+-----------+--------+----------------------------------------+
| 1 | 1 | Hello! This is Praveen! Stop spamming! |
+-----------+--------+----------------------------------------+
| 2 | 1 | Hey CrazyGuy, stop your spams!!! |
+-----------+--------+----------------------------------------+
| 3 | 3 | SPAM! SPAM!! SPAM!!! |
+-----------+--------+----------------------------------------+
IndexTable
+---------+-----------+------------+---------------------+
| IndexID | IndexType | IndexRowID | IndexTime |
+---------+-----------+------------+---------------------+
| 1 | 1 | 1 | 2015-04-10 10:50:00 |
+---------+-----------+------------+---------------------+
| 2 | 1 | 2 | 2015-04-10 10:55:00 |
+---------+-----------+------------+---------------------+
| 3 | 2 | 1 | 2015-04-10 11:25:00 |
+---------+-----------+------------+---------------------+
| 4 | 3 | 1 | 2015-04-10 11:30:00 |
+---------+-----------+------------+---------------------+
| 5 | 2 | 2 | 2015-04-10 11:45:00 |
+---------+-----------+------------+---------------------+
TableNames
+---------+-----------+
| TableID | TableName |
+---------+-----------+
| 1 | Users |
+---------+-----------+
| 2 | Messages |
+---------+-----------+
| 3 | Comments |
+---------+-----------+
I am more interested in the Index table to list all the activities. So, if I give a query like this:
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName` FROM `IndexTable` ORDER BY `IndexTime` DESC;
I would get all the contents like this:
+---------+-----------+------------+---------------------+------------+
| IndexID | IndexType | IndexRowID | IndexTime | IndexTable |
+---------+-----------+------------+---------------------+------------+
| 5 | 2 | 2 | 2015-04-10 11:45:00 | Messages |
+---------+-----------+------------+---------------------+------------+
| 4 | 3 | 1 | 2015-04-10 11:30:00 | Comments |
+---------+-----------+------------+---------------------+------------+
| 3 | 2 | 1 | 2015-04-10 11:25:00 | Messages |
+---------+-----------+------------+---------------------+------------+
| 2 | 1 | 2 | 2015-04-10 10:55:00 | Users |
+---------+-----------+------------+---------------------+------------+
| 1 | 1 | 1 | 2015-04-10 10:50:00 | Users |
+---------+-----------+------------+---------------------+------------+
If you see the result, the last column shows the Table Names and the concerned Primary Key (Item ID) of the table too. So, with the above result, I wanna add a column, that selects the main value from the table, with the ID specified.
In short, I would like the query to be:
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName`, (
SELECT {Username OR MessageContent OR CommentContent}
FROM {`IndexTypeName`}
WHERE {`UserID` OR `MessageID` OR `CommentID`} = `IndexRowID`
) AS `TableValue` FROM `IndexTable`
ORDER BY `IndexTime` DESC;
Is it possible with MySQL-Server?
using CASE WHEN:
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName`,
CASE
WHEN IndexType=1 THEN (SELECT Username FROM Users WHERE IndexRowID=UserID)
WHEN IndexType=2 THEN (SELECT MessageContent FROM Messages WHERE IndexRowID=MessageID)
WHEN IndexType=3 THEN (SELECT CommentContent FROM Comments WHERE IndexRowID=CommentID) END TableValue
ORDER BY `IndexTime` DESC;
The better solution is to put the data from those different tables in one table and use the typeid to separate them
I have searched and gone through the available topics similar to mine. But, failed to find that satisfies my requirements. Hence, posting it here.
I have four tables as follows:
"Organization" table:
--------------------------------
| org_id | org_name |
| 1 | A |
| 2 | B |
| 3 | C |
"Members" table:
----------------------------------------------
| mem_id | mem_name | org_id |
| 1 | mem1 | 1 |
| 2 | mem2 | 1 |
| 3 | mem3 | 2 |
| 4 | mem4 | 3 |
"Resource" table:
--------------------------------
| res_id | res_name |
| 1 | resource1 |
| 2 | resource2 |
| 3 | resource3 |
| 4 | resource4 |
"member-resource" table:
--------------------------------------------
| sl_no | mem_id | res_id |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 4 | 3 |
| 5 | 3 | 4 |
| 6 | 2 | 3 |
| 7 | 4 | 3 |
I want to find out the total number of distinct resources according to organizations. Expected output is as follows:
| org_name | Total Resources |
| A | 3 |
| B | 1 |
| C | 1 |
I also want to find out the total number of shared resources according to organizations. Expected output is as follows:
| org_name | Shared Resources |
| A | 1 |
| B | 0 |
| C | 1 |
Any help in this regard will highly be appreciated.
Regards.
It is much simpler than you think, particularly because you don't even need the resource table:
SELECT o.org_name, COUNT(DISTINCT mr.res_id) TotalResources
FROM member_resource mr
JOIN members m ON mr.mem_id = m.mem_id
JOIN organization o ON m.org_id = o.org_id
GROUP BY o.org_id
Output:
| ORG_NAME | TOTALRESOURCES |
|----------|----------------|
| A | 3 |
| B | 1 |
| C | 1 |
Fiddle here.
Try this query below.
SELECT org_name, COUNT(DISTINCT res_id)
FROM organization, members, member-resource
WHERE members.mem_id = member-resource.mem_id
AND organization.org_id = members.org_id
GROUP BY org_id, org_name