I have one database here
1st table is conversation table
___________________________
| id | conversation Name |
__________________________
| 1 | member and officer |
___________________________
| 2 | member and guests |
___________________________
2nd table is user table
_________________
| id | name |
_________________
| 1 |member |
_________________
| 2 |officer |
_________________
| 3 |guest |
_________________
3rd table is conversation_user_involved table
_________________________________
| id_conversation | id_user |
_________________________________
| 1 | 1 |
_________________________________
| 1 | 2 |
_________________________________
| 2 | 1 |
_________________________________
| 2 | 3 |
_________________________________
The question is how do I select conversation based on user.
Let say the input user parameter is 1 and 2 so that mean user 1 is member and 2 is officer.
How can I select the conversation member and officer?
I have tried "IN" query but it didnt work.
untested:
select id_conversation, conversation_Name
from conversation_user_involved
inner join conversation on (id_conversation = conversation.id)
where id_user in (1, 2)
group by id_conversation
having count(id_conversation) = 2
Related
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
Basically, I have a table which looks like this:
Table: People
------------------------
ID | Employee
------------------------
1 | Saul
2 | Jess
3 | Kenny
And then I have another table that looks like this:
Table: Projects
------------------------
ID | Project | Employee
------------------------
1 | pro1 | 3
2 | pro2 | 1
3 | pro3 | 2
The problem is that I want to replace the numbers in the Employee column of the Projects table with the corresponding names of the employees in the People table.
Thanks in advance.
You don't need to replace the ID Number, you need to build a query to show the results you want.
An example query can be:
SELECT Projects.ID, Projects.Project, People.Employee
FROM Projects
INNER JOIN People on Projects.Employee = People.ID
This will result in:
+----+---------+----------+
| ID | Project | Employee |
+----+---------+----------+
| 1 | pro1 | Kenny |
| 2 | pro2 | Saul |
| 3 | pro3 | Jess |
+----+---------+----------+
I need a help with mySQL SELECT query from multiple tables. I have got four tables: school, discipline, pupils and teams.
School table looks like:
+------+---------+---------------+----------+
| id | name | discipline_id | pupil_id |
+------+---------+---------------+----------+
| 1 | one | 2 | 5 |
+------+---------+---------------+----------+
| 2 | two | 3 | 8 |
+------+---------+---------------+----------+
| 3 | three | 4 | 12 |
+------+---------+---------------+----------+
Discipline table looks like:
+------+---------+
| id | name |
+------+---------+
| 1 | math |
+------+---------+
| 2 | bio |
+------+---------+
| 3 | liter |
+------+---------+
| 4 | geo |
+------+---------+
Teams table looks like:
+------+---------+---------------+-----------+
| id | name | school_id | member_id |
+------+---------+---------------+-----------+
| 1 | T1 | 1 | 3 |
+------+---------+---------------+-----------+
| 2 | T2 | 3 | 3 |
+------+---------+---------------+-----------+
| 3 | T3 | 2 | 9 |
+------+---------+---------------+-----------+
The result of disciplines I need to get with a "SELECT from discipline..." query by "member_id = 3" is:
+-----------------+---------------+
| discipline_name | discipline_id |
+-----------------+---------------+
| bio | 2 |
+-----------------+---------------+
| geo | 4 |
+-----------------+---------------+
By matching member's school and then getting its discipline, if it makes sense...Is it possible to do with just one mySQL query?
Type of: member_id 3 => school_id 1,3 => discipline_id = show related disciplines names and ids which are 2, 4
Thank you very much...
Your goal is not clear or makes no sense to me.
But here is what you are literally asking for:
SELECT
s.discipline_id
d.name
FROM teams t
LEFT JOIN school s
ON s.id = t.school_id
LEFT JOIN discipline d
ON d.id = s.discipline_id
WHERE t.member_id = 3
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've got two tables in my database. Table 1 is a list of "timelines" and their corresponding owners and title.
Table 2 is a list of users who have access to the timelines but are followers, not owners.
I'm trying to write a query that outputs the lineID's and corresponding titles that are linked to a userID in either of the two tables.
A query for userID 1 would ideally output:
1 a
2 b
3 c
6 f
Hopefully this isn't too confusing but the purpose is to fill a dynamically generated select box with the LineID and Title for a given UserID...
Table 1 ("owners")
--------------------------
| LineID | UserID | Title |
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 1 | c |
| 4 | 2 | d |
| 5 | 2 | e |
| 6 | 1 | f |
--------------------------
Table 2 ("followers")
----------------------------
| RowID | LineID | UserID |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 1 |
| 4 | 3 | 2 |
| 5 | 2 | 2 |
| 6 | 6 | 1 |
----------------------------
I tried using:
SELECT title
FROM `lines`
LEFT JOIN follow
ON follow.user_id = lines.user_id
WHERE follow.user_id = 1
That ended up producing duplicate rows.
The output I need would ideally be an array consisting of all the lineID's and Titles associated with that userID.
select LineId, Title
from owners
where LineId in (select LineId from followers group by LineId )
order by owners.LineId