here is two tables:
a:
+-----+------------------------+
| id | conten |
+-----+------------------------+
| 1 | q. |
| 2 | q. |
| 3 | s. |
| 4 | g |
| 1 | a |
| 2 | a |
+-----+------------------------+
b:
+-----+------------------------+
| id | type |
+-----+------------------------+
| 1 | I. |
| 2 | II. |
| 3 | III. |
| 4 | IV |
| 5 | V |
| 6 | VI |
+-----+------------------------+
Is there a way to select from a and b so that for one id 2, there will be one additional field that groups all content from that id? the select result should be something like this:
+-----+------------------------+-----------+
| id | type | contents |
+-----+------------------------+-----------+
| 2 |I. | q,a |
+-----+------------------------+-----------+
Edited
btw, if there is a way to do it by sqlahcmey, that would be sweet.
SELECT b.id, b.type, IFNULL(GROUP_CONCAT(a.conten), '') AS contents
FROM b
LEFT JOIN a ON a.id = b.id
GROUP BY b.id
See How do I write a group_concat function in sqlalchemy? for how to translate GROUP_CONCAT to SQLAlchemy.
Related
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;
I have two tables, A and B
table A has this columns:
id, title
table B has these columns:
id, content, A_id, type
In table B, A_id is foreign key related to table A.
I want to write a search query that search title in A and left Join to B where A.id = B.A_id and search B.content
It is so easy:
SELECT A.*, B.content FROM A LEFT JOIN B ON A.id=B.A_id
WHERE A.title like 'sometitle' AND B.content like 'somecontent';
But I have column 'type' in table B that have entries like this: 'good','bad','ugly','good','good'...
when I search 'content' in table B I want that every content to be related to a 'type'. In other words, the query should give me result related to content1 and type good And content2 and type bad...
Example:
TAble A:
+----+--------+
| id | title |
+----+--------+
| 1 | white |
| 2 | blue |
| 3 | red |
| 4 | white |
| 5 | blue |
+----+--------+
table B:
+----+---------+------+------+
| id | content | A_id | type |
+----+---------+------+------+
| 1 | dog | 1 | good |
| 2 | dog | 1 | bad |
| 3 | cat | 2 | good |
| 4 | cat | 2 | bad |
| 4 | cat | 2 | ugly |
| 6 | crow | 3 | good |
| 7 | crow | 3 | bad |
| 8 | crow | 3 | ugly |
| 9 | mouse | 2 | good |
| 10 | zebra | 3 | bad |
| | | | |
+----+---------+------+------+
I want a query that that its output be this:
+----+-------+---------+------+
| id | title | content | type |
+----+-------+---------+------+
| 1 | white | dog | good |
| 2 | blue | cat | bad |
+----+-------+---------+------+
SQL HERE
Your query for the expected result was almost correct, just you used the wrong column content whether the correct column would be type. Look at the below query :
SELECT A.id, A.title, B.content, B.type FROM A
LEFT JOIN B ON A.id=B.A_id
WHERE (a.title='white' AND B.type='good')
OR (a.title='blue' AND B.type='bad')
ORDER BY a.id asc
OUTPUT
+----+-------+----------+------+
| id | title | content | type |
+----+-------+----------+------+
| 1 | white | content5 | good |
| 3 | blue | content6 | bad |
| 4 | white | content3 | good |
| 5 | blue | content9 | bad |
+----+-------+----------+------+
I think the query you posted at the end of your question should start like this:
SELECT A.id, A.title, B.A_id, B.content, B.type .... (etc.)
so that also B.A_id is fetched
Database 1 (2 tables) : sandbox_maesc4
table 1: coordinates
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
table 2: workstation_userlogged
+----------------------------------+
| id | ws | user |
+----------------------------------+
| 1 | COMP123 | ryan |
| 2 | COMP345 | luda |
| 3 | COMP567 | chris |
| 4 | COMP891 | michel|
| 5 | COMP444 | isabel|
+----------------------------------+
Database 2 (1 table): softphone_materials_updater
Table 1: workstation
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
Problem:
I only have read access in Database 2.
So I did a SELECT query for both fields and created the table "userlogged".
Now, I want to combine both tables "coordinates" and "userlogged" by joining table "workstation"
with their relation of the "station_number" field and "pod" field from. How can I achieve this? Below is the query that I tried but doesnt work.
I have extra fields in "coordinates" table (X,Y fields with actual coordinates). In PHP I use all fields to show them on screen.
SELECT
coordinate_id,
section_name,
x_coord,
y_coord,
ws.username,
ws.hostname,
w.pod,
FROM
sandbox_maesc4.coordinates c,
sandbox_maesc4.workstation_userlogged ws
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod
I think maybe this is what you want?
SELECT
coordinate_id,
section_name,
x_coord,
y_coord,
wsu.username,
wsu.hostname,
w.pod
FROM
sandbox_maesc4.coordinates c
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod
INNER JOIN
sandbox_maesc4.workstation_userlogged wsu
ON w.ws = wsu.ws
Not sure about the database and table names, they seem to differ between your sample query and the description.
Please be assured that I searched a lot on SE for an answer similar to mine but didn't get any good result and here I am asking for some help.
I have 3 tables as follows:
Table Professors:
+---------+--------+
| idProf | name |
+---------+--------+
| 1 | Ben |
| 2 | John |
| 3 | Bob |
+---------+--------+
Table Classes:
+---------+--------+------------+
| idClass | name | profRefId |
+---------+--------+------------+
| 1 | French | 1 |
| 2 | English| 1 |
| 3 | German | 3 |
| 4 | Science| 2 |
+---------+--------+------------+
Table Lessons:
+----------+----------+--------------+
| idLesson | name | classRefId |
+----------+----------+--------------+
| 1 | Lesson1 | 1 |
| 2 | Lesson2 | 1 |
| 3 | Lesson3 | 2 |
| 4 | Lesson4 | 4 |
| 5 | Lesson5 | 4 |
| 6 | Lesson6 | 3 |
+----------+----------+--------------+
Now, what I was struggling to achieve is:
I pass idProf as a URL parameter ($_GET['idProf'])
And I would like the right SQL statement based on that param to list all the classes for that professor and inside each class list its lessons.
Something that will look like this on a webpage:
Something like this?
SELECT a.name ProfessorName, b.name ClassName, c.name LessonName
FROM Professors a
INNER JOIN Classes b
ON a.idProf=b.profRefID
INNER JOIN Lessons c
ON b.idClass=c.classRefID
I have two tables like below,
mysql> select * from Books ;
+----+------+------------+----------+----------+
| id | name | author_name| category | category2|
+----+------+------------+----------+----------+
| 1 | 1 | Steve | CT001 | CT003 |
| 2 | 2 | John | CT002 | CT002 |
| 3 | 3 | Larry | CT003 | CT002 |
| 4 | 3 | Michael | CT004 | CT004 |
| 5 | NULL | Steven | CT005 | CT005 |
+----+------+------------+----------+----------+
mysql> select * from Codemst ;
+----+------+------------+
| id | code | name |
+----+------+------------+
| 1 | CT001| fiction |
| 2 | CT002| category1 |
| 3 | CT003| etc |
| 4 | CT004| etc2 |
| 5 | CT005| etc3 |
+----+------+------------+
I want to get human readable category name when I query like "select * from Books;"
If there was only one category in the Books table, I think I can use "Join" but, in this case what can I do?
select * from Books b
Inner Join Codemst c1 on b.category = c1.code
inner join codemst c2 on b.category2 = c2.code;
c1.name will hold the readable category, and c2.name the readable category2