I have following tables:
table users - PRIMARY KEY (user_id)
+---------+----------+-----------+
| user_id | username | realname |
+---------+----------+-----------+
| 1 | peterpan | Peter Pan |
| 2 | bobfred | Bod Fred |
| 3 | sallybe | Sally Be |
| 6 | petersep | Peter Sep |
+---------+----------+-----------+
table users_groups - PRIMARY KEY (user_id, group_id)
+---------+----------+
| user_id | group_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 6 |
| 3 | 9 |
| 6 | 6 |
| 6 | 9 |
+---------+----------+
table game - PRIMARY KEY (id)
+----+-------+
| id | game |
+----+-------+
| 1 | Game1 |
| 2 | Game2 |
| 6 | Game6 |
| 9 | Game9 |
+----+-------+
table groups - PRIMARY KEY(group_id)
+----------+--------------+---------------+
| group_id | group_name | group_desc |
+----------+--------------+---------------+
| 1 | Groupname1 | Description1 |
| 2 | Groupname2 | Description2 |
+----------+--------------+---------------+
table group_game - PRIMARY KEY(group_id, game_id)
+----------+----------+
| group_id | game_id |
+----------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 6 |
| 2 | 9 |
+----------+----------+
I want to display this (like a group list):
+----+------------+--------------+---------------------+--------------+
| id | group name | group desc | group members | group games |
+----+------------+--------------+---------------------+--------------+
| 1 | GroupName1 | Description1 | Peter Pan, Bob Fred | Game1, Game2 |
| 2 | GroupName2 | Description2 | Sally Be, Peter Sep | Game6, Game9 |
+----+------------+--------------+---------------------+--------------+
Now I have this query but it gives me no rows (no error, just zero rows):
SELECT
g.group_name,
g.group_id,
g.group_desc,
GROUP_CONCAT(DISTINCT ga.game SEPARATOR ', ') AS games,
GROUP_CONCAT(DISTINCT u.realname SEPARATOR ', ') AS users
FROM groups g
LEFT JOIN users_groups ug1
ON g.group_id=ug1.group_id
LEFT JOIN users u
ON ug1.user_id=u.user_id
LEFT JOIN group_game gg
ON g.group_id=gg.group_id
LEFT JOIN game ga
ON gg.game_id=ga.id
GROUP BY g.group_name
How can I solve this problem or how can I write this query?
I just want to show a group list with all information (like group information, users of the groups, games of this group).
Related
student table
|----------------------|
| student_id | name |
|------------|---------|
| 1 | Richard |
| 2 | Emily |
| 3 | Hans |
|------------|---------|
lecturer table
|--------------------|
| lecturer_id | name |
|-------------|------|
| 1 | John |
| 2 | Mike |
|-------------|------|
classes table
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 1 | 1 | Basic of algorithm |
| 2 | 1 | Basic of programming |
| 3 | 2 | Database Essentials |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
attendance table
|-----------------------|
| class_id | student_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 4 | 1 |
| 4 | 2 |
|----------|------------|
how to show classes records (from classes table) that not attended by Hans (student) in MySQL?
desired result :
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 2 | 1 | Basic of programming |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
One approach uses EXISTS:
SELECT c.class_id, c.lecturer_id, c.material
FROM classes c
WHERE NOT EXISTS (SELECT 1 FROM attendance a
INNER JOIN student s
ON a.student_id = s.student_id
WHERE a.class_id = c.class_id AND
s.name = 'Hans');
Using joins -
select c.class_id
from attendance a inner join student s on (a.student_id=s.student_id and s.student_id='Hans')
right outer join classes c on (a.class_id=c.class_id)
where a.class_id is null
I try to write a little voting tool. I have 3 tables: users, locations and votes. votes has 2 foreign keys (user_id and location_id).
Users (example data):
+----+----------+
| id | username |
+----+----------+
| 5 | user1 |
| 7 | user2 |
| 11 | user3 |
| 4 | user4 |
| 12 | user5 |
+----+----------+
Locations:
+----+----------------+
| id | locationname |
+----+----------------+
| 1 | Pasta |
| 2 | Burger |
| 3 | Pizza |
| 4 | Chinese |
| 5 | Thai |
+----+----------------+
Votes:
+----+---------+-------------+------------+
| id | user_id | location_id | date |
+----+---------+-------------+------------+
| 30 | 5 | 1 | 2016-06-30 |
| 31 | 5 | 1 | 2016-07-01 |
| 32 | 7 | 1 | 2016-07-01 |
| 38 | 11 | 2 | 2016-07-01 |
| 39 | 4 | 1 | 2016-07-04 |
| 41 | 12 | 3 | 2016-07-04 |
| 44 | 5 | 4 | 2016-07-04 |
| 46 | 7 | 5 | 2016-07-04 |
+----+---------+-------------+------------+
The keypair date & user is unique so a user can't vote twice.
I now want to have a list like this for CURDATE():
+----------------+----------------+----------------------+
| locationname | Votes | Voters |
+----------------+----------------+----------------------+
| Pasta | 3 | user1, user2, user x |
| Burger | 2 | user3, user4 |
| Pizza | 1 | user5 |
| Chinese | 1 | user6 |
| Thai | 0 | |
+----------------+----------------+----------------------+
How can I solve this? Tried something like that:
SELECT locations.locationname AS location, count(*) AS count, GROUP_CONCAT(users.username SEPARATOR ', ') AS Voters
FROM votes
INNER JOIN locations ON votes.location_id=locations.id
WHERE date = CURDATE()
INNER JOIN users ON users.id=votes.user_id
WHERE location_id = "1" AND date = CURDATE()
GROUP BY location_id
ORDER BY count DESC;
Thanks
A friend of mine showed me how to solve this problem:
SELECT l.id AS locationid, l.locationname, count(username) AS count, GROUP_CONCAT(username SEPARATOR ", ") AS users
FROM locations l
LEFT JOIN votes v
ON v.location_id = l.id AND v.date = CURDATE()
LEFT JOIN users u
ON v.user_id = u.id
GROUP BY locationname
ORDER BY count DESC;
There are my two tables:
Table name: FRIENDS
+-------+---------+---------+-------------------+
| id | firstName | lastName | city |
+-------+--------------+------------+-----------+
| 1 | dudi | edri | london |
| 2 | maor | azulay | madrid |
| 3 | batel | azulay | tel aviv |
| 4 | nir | cohen | barcelona |
| 5 | evia | perez | miami |
| 6 | neria | perez | new-york |
| 7 | nevo | kakoun | roma |
+-------+---------+---------+-------------------+
Table name: ORDERS
+-------+---------+---------+-----------------+
| id | firstName | amount | status |
+-------+--------------+----------+-----------+
| 1 | dudi | 5684 | shipped |
| 2 | maor | 4896 | shipped |
| 3 | batel | 2496 | delay |
+-------+--------------+----------+-----------+
my question is:
I want the friends that have no order.
The answer:
| 4 | nir | cohen | barcelona |
| 5 | evia | perez | miami |
| 6 | neria | perez | new-york |
| 7 | nevo | kakoun | roma |
+-------+---------+---------+-------------------+
how i wrote the query with inner join.
Thanks.
You shouldn't use firstName in ORDERS as Foreign Key. The foreign key should reference the primary key. Use ID from FRIENDS like:
Table name: ORDERS
+-------+---------+---------+-----------------+
| id | friendID | amount | status |
+-------+--------------+----------+-----------+
| 1 | 1 | 5684 | shipped |
| 2 | 2 | 4896 | shipped |
| 3 | 3 | 2496 | delay |
+-------+--------------+----------+-----------+
And query using LEFT OUTER JOIN:
SELECT f.*
FROM FRIENDS f
LEFT JOIN ORDERS o
ON f.ID = o.friendID
WHERE o.ID IS NULL;
LiveDemo
Another possibility is to use correlated subquery:
SELECT f.*
FROM FRIENDS f
WHERE NOT EXISTS (SELECT 1
FROM orders o
WHERE o.friendID = f.ID);
LiveDemo2
Tables:
CREATE TABLE friends(
id INTEGER NOT NULL PRIMARY KEY -- you can add AUTO_INCREMENT if needed
...
);
CREATE TABLE orders(
id INTEGER NOT NULL PRIMARY KEY
,friendID INTEGER NOT NULL
,FOREIGN KEY (friendID) REFERENCES friends(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 mysql database with two tables.
First (information)
+---------+------+----------+
| species | sex | user |
+---------+------+----------+
| bird | NULL | 1 |
| bird | f | 1 |
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| dog | m | 2 |
| hamster | f | 2 |
| snake | m | 1 |
+---------+------+----------+
Second (users)
+--------+-----+
| user | id |
+--------+-----+
| amy | 1 |
| dav | 2 |
| mot | 3 |
| mmm | 4 |
| aaw | 5 |
| dsa | 6 |
+--------+-----+
I want to count and show values from table "information" for each user row on table "users"
Like this
+---------+------+----------+
| user | id | count |
+---------+------+----------+
| amy | 1 | 6 |
| dav | 2 | 2 |
| mot | 3 | 0 |
| mmm | 4 | 0 |
| aaw | 5 | 0 |
| dsa | 6 | 0 |
+---------+------+----------+
How can I do this query?
select users.user, users.id, count (species.name)
from users left join species
on users.id = species.user
group by users.user, users.id
order by count (species.name) desc
Isn't it something like:
select u.user, u.id, count(i.user)
from user u
inner join information i on i.user = u.id
group by u.user, u.id