I have 3 tables
QUESTION table with below 2 properties
1. ID (serial)
2. Question (varchar)
ANSWER table with below 4 properties
1. ID (serial)
2. QuestionID (foreign key to table QUESTION)
3. StudentID (foreign key to table STUDENT)
4. ANSWER (varchar)
5. SubmitDateTime (datetime)
STUDENT table with below properties
1. ID (serial)
2. Name (varchar)
I just want to show records with each student (one record for each student) with every answer. If any question's answer is not given by the student it will show blank.
For example:
QUESTION TABLE
| ID | QUESTION |
|----- | --------- |
| 1 | A FOR? |
| 2 | B FOR? |
| 3 | C FOR? |
ANSWER TABLE
| ID | QuestionID | StudentID | ANSWER | SubmitDateTime |
|----- | ----------- |------------|--------|----------------|
| 1 | 1 | 1 |Apple | something date |
| 2 | 1 | 2 |Ant | something date |
| 3 | 2 | 1 |Book | something date |
| 4 | 3 | 2 |Cat | something date |
STUDENT TABLE
| ID | NAME |
|----- | --------- |
| 1 | Jhon |
| 2 | Lily |
Expected Records
Result table
| ID | NAME | Answers |
|----- | --------- | ---------------------|
| 1 | Jhon | Apple,Book,<blank> |
| 2 | Lily | Ant,<blank>,Cat |
"blank" means no record will be shown instead of a blank space or a hyphane.
My implementation:
SELECT s.ID,s.Name,
GROUP_CONCAT(a.answer SEPARATOR ',') AS answers
FROM student AS s
LEFT JOIN answer AS a ON a.studentID=s.ID
WHERE a.submitdate BETWEEN '<somedate>' AND '<somedate>'
GROUP BY s.ID ORDER BY a.ID ASC
It does not give me a blank answer. How to get these?
Try this:
SELECT sid, sname, GROUP_CONCAT(IFNULL(a.answer,'-') ORDER BY qid)
FROM
(SELECT q.id AS qid, s.id AS sid, s.Name AS sname
FROM question q CROSS JOIN student s) qs
LEFT JOIN answer a ON qs.qid=a.QuestionID AND qs.sid=a.StudentID
GROUP BY sid, sname;
The base query is a CROSS JOIN between question and student tables that will be a subquery and give a result like this:
+-----+-----+-------+
| qid | sid | sname |
+-----+-----+-------+
| 1 | 2 | Lily |
| 1 | 1 | Jhon |
| 2 | 2 | Lily |
| 2 | 1 | Jhon |
| 3 | 2 | Lily |
| 3 | 1 | Jhon |
+-----+-----+-------+
As you can see, each of the student will be paired with all of the existing question regardless of their answer records in answer table. This will be the reference for the LEFT JOIN with answer table.
Demo fiddle
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Retrieving the last record in each group - MySQL
(33 answers)
Closed 4 months ago.
I not figure out to evaluate the maximum creation date value in a join. Below the tables envolved:
Supervisor
SupervisorCompany
Company
A Supervisor can be related to many Company, and a Company can be related to many Supervisor.
The relation ManyToMany is represented by SupervisorCompany table that conatains the foreign key related.
--------------
| Supervisor |
-----------------------------------------------------------
IdSupervisor | Name | Surname | CreationTime |
------------------------------------------------------------
1 | Maximilian | Green | 2022-01-01 01:00:01 |
------------------------------------------------------------
2 | Josh | Nice | 2023-04-03 01:00:01 |
------------------------------------------------------------
3 | Albert | Cloud | 2022-03-01 01:32:01 |
------------------------------------------------------------
4 | Peter | Dark | 2022-03-01 01:32:01 |
------------------------------------------------------------
--------------
| Company |
--------------------------------------
IdCompany | Brand | Address |
--------------------------------------
1 | X | |
--------------------------------------
2 | Y | |
--------------------------------------
3 | Z | |
--------------------------------------
4 | J | |
--------------------------------------
-------------------------
| SupervisorCompany |
--------------------------------------
Id |Id_Supervisor | Id_Company |
--------------------------------------
8 | 1 | 1 |
--------------------------------------
9 | 2 | 1 |
--------------------------------------
10 | 3 | 1 |
--------------------------------------
11 | 4 | 3 |
--------------------------------------
I want return the newest CreationTime Supervisor of a Company for each Company.
I executed this query:
select *, MAX(Supervisor.CreationTime) from Company
inner join SupervisorCompany on Company.IdCompany = SupervisorCompany.IdCompany
inner join Supervisor on SupervisorCompany.IdSupervisor = Supervisor.IdSupervisor
GROUP BY Company.IdCompany;
But the result is:
| Maximilian | Green | 2022-01-01 01:00:01 | 1 |
insted
| Josh | Nice | 2023-04-03 01:00:01 | 1 |
I know that there is somethings wrong in the query but I don't know exactly what is the mistake.
I tried with a subquery or others approach but I don't figure out.
Thanks in advance
You should select only the company ID and the max creation time:
SELECT c.IdCompany, MAX(s.CreationTime) AS MaxCreationTime
FROM Company c
INNER JOIN SupervisorCompany sc ON c.IdCompany = sc.IdCompany
INNER JOIN Supervisor s ON sc.IdSupervisor = s.IdSupervisor
GROUP BY c.IdCompany;
The rough rule of thumb for an aggregation query is that we can select only columns which appear in the GROUP BY clause or columns which appear inside aggregate functions (such as MAX()).
I have two MySQL tables (table_a and table_b) and a join table (table_c).
Table Structures:
table_a:
__________________
| table_a: |
|----------------|
| id |
| result_column |
------------------
table_b:
__________________
| table_b: |
|----------------|
| id |
| name |
------------------
table_c:
__________________
| table_c: |
|----------------|
| id |
| table_a_id |
| table_b_id |
------------------
My Goal:
I want to find a query that will:
Iterate over every table_a record and get the table_a.id value
Find any records in table_c which have a matching table_c.table_a_id value
For each matching record in table_c get the table_c.table_b_id value
Find the record in table_b which has a matching table_b.id value
For that matching record in table_b get the table_b.name value
In table_a, concatenate each matched name value into the corresponding table_a.result_column
Example:
Before the Query:
_______________________ _________________________________ ________________
| table_a: | | table_c: | | table_b: |
|---------------------| |-------------------------------| |--------------|
| id | result_column | | id | table_a_id | table_b_id | | id | name |
|-----|---------------| |-----|------------|------------| |-----|--------|
| 1 | | | 1 | 1 | 3 | | 1 | Kevin |
| 2 | | | 2 | 1 | 4 | | 2 | Jesse |
| 3 | | | 3 | 2 | 2 | | 3 | Karen |
----------------------- | 4 | 3 | 1 | | 4 | Tim |
| 5 | 3 | 5 | | 5 | Lauren |
--------------------------------- ----------------
After the Query:
_______________________ _________________________________ ________________
| table_a: | | table_c: | | table_b: |
|---------------------| |-------------------------------| |--------------|
| id | result_column | | id | table_a_id | table_b_id | | id | name |
|-----|---------------| |-----|------------|------------| |-----|--------|
| 1 | Karen, Tim | | 1 | 1 | 3 | | 1 | Kevin |
| 2 | Jesse | | 2 | 1 | 4 | | 2 | Jesse |
| 3 | Kevin, Lauren | | 3 | 2 | 2 | | 3 | Karen |
----------------------- | 4 | 3 | 1 | | 4 | Tim |
| 5 | 3 | 5 | | 5 | Lauren |
--------------------------------- ----------------
For absolute clarity, I understand that this is incredibly bad practice within a relational data-table. This is as far from normalization as one can get. I would never design a database like this. I was tasked with creating a custom column with a list of values purely for a business case.
The query you seem to want is:
select c.table_a_id, group_concat(b.name separator ', ')
from c join
b
on c.table_b_id = b.id
group by c.table_a_id;
If you actually want to update a, you can put this into an update statement:
update a join
(select c.table_a_id, group_concat(b.name separator ', ') as names
from c join
b
on c.table_b_id = b.id
group by c.table_a_id
) cb
on cb.table_a_id = a.id
set result_column = cb.names
Previous answer is close; but you also required that you only want the records matched in table C that are in A.
The first query does not meet this requirement; but the update statement does, as it will only update records in table A, if the id matches the table_a_id value pulled from table C.
Given what you said you wished for the end result, the update statement above would work.
If you wish to be explicit in your logic, just add a join from table A to table C.
select a.id, group_concat(b.name separator ', ')
from a
join c ON (a.id = c.table_a_id)
join b ON (c.table_b_id = b.id)
group by a.id;
This question already has answers here:
Select values that meet different conditions on different rows?
(6 answers)
Closed 4 years ago.
can I return a single instance of a row after using a join on a categories table.
Entries
| id | Name |
| 1 | Johnny |
| 2 | Steve |
| 3 | Bam |
Categories
| cat_id | Name |
| 1 | Season one |
| 2 | Season two |
| 3 | Season three|
Category Posts
| id | cat_id |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
What I want to do is select all cast where members that have been in season 2 and 3, they must have been in both and I only want a single instance returned.
Expected output
| id | Name |
| 1 | Johnny |
| 2 | Steve |
How would I got about selecting these? I've thought about grouping the user based on their name however because I'm selecting IN ("2", "3") I get some users that have been in two but not three and the expected results are wrong.
Thanks
you can use sub query
SELECT id,name from Entries where id in (select a.id from (select id from
category_post WHERE cat_id=2) as a,(select id from category_post WHERE cat_id=3) as b where a.id=b.id)
I have 2 tables like this:
Table person
id | name
---------
1 | john
2 | mike
3 | carl
4 | keny
5 | anna
Table vehicle
owner | vechicle
----------------
1 | RTA457
3 | GSW684
3 | GKI321
3 | SNE798
5 | YTT662
So, I want to make a query joining both tables, something like this:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner
Getting these results
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
4 | keny | NULL | NULL
5 | anna | 5 | YTT662
Finally, I want to limit it to 3 persons, showing all their vehicles, like this:
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
There is any way to do it?
May help with a subquery
SELECT
*
FROM
(SELECT * FROM person LIMIT 3) t
LEFT JOIN vehicle ON t.id = vehicle.owner
Didn't try it, but something like this:
SELECT * FROM person
LEFT JOIN vehicle ON person.id = vehicle.owner
WHERE person.id IN (SELECT ID FROM PERSON LIMIT 3);
You could simply have your query as such:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner LIMIT 10;
This SO could be handy as well. Hope this helps!
I need to join 3 different MySQL tables:
The questions table (represents a questio)
The questions votes table (represents a like to the question when Sign=1 and an unlike when Sign=0)
The answers table (represents a table with the answers to an specific question).
The three tables have this structure:
Questions
--------------------------------------------
| ID | UserID | Title | Body | Date
--------------------------------------------
| 1 | 32 | Is it raining? | BodyQuestion | 01/01/2016
| 2 | 45 | Who are we? | BodyQuestion | 02/02/2016
--------------------------------------------
QuestionsVotes
--------------------------------------------
| ID | QuestionID | Sign
--------------------------------------------
| 1 | 1 | 1
| 2 | 2 | 1
| 3 | 1 | 0
| 4 | 1 | 1
| 5 | 2 | 0
Answers
--------------------------------------------
| ID | QuestionID | UserID | Body
--------------------------------------------
| 1 | 1 | 45 | Yes, it is.
| 2 | 2 | 10 | Tricky question...
| 3 | 1 | 67 | In Barcelona it is not
What I need is a SQL query that returns, given the ID of a Question, to return as much rows as number of answers the question has received each one with the Questions.ID, Questions.UserID, Questions.Title, Questions.Body, Questions.Date, the likes associated to the question, the unlikes associated with the question, Answers.ID, Answers.UserID. As you see, the only change between the rows are the Answers fields.
SQL Output (for Question with ID=1)
--------------------------------------------
| ID | UserID | Title | Body | Date | Likes | Unlikes | AnswerID | AnswerUserID
--------------------------------------------
| 1 | 32 | Is it raining? | BodyQuestion | 01/01/2016 | 2 | 1 | 1 | 45
| 1 | 32 | Is it raining? | BodyQuestion | 01/01/2016 | 2 | 1 | 3 | 67
EDIT 1: Example given.
Try this:
SELECT `ID` , `UserID` , `Title`, `Body`, `Date`, `Likes`, `Unlikes`, `AnswerID`, `AnswerUserID`
FROM QUESTIONS q
INNER JOIN ANSWERS a ON q.`ID` = a.`QuestionID`
INNER JOIN QuestionVotes v ON q.`ID` = v.`QuestionID`