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
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;
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.
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 a db in mysql with multiple tables and would like to join multiple tables into one view to save me from having to build 3 or 4 sql statements or even one large joining statement in php to get the same info.
Here are all my tables that I want to join
track_title
+----+------------------+
| ID | TITLE |
+----+------------------+
| 1 | Title Here |
| 2 | Another Title |
| 3 | Some Other Title |
+----+------------------+
track_artist
+----+----------------+-----------+-----------+
| ID | TRACK_TITLE_ID | ARTIST_ID | SYMBOL_ID |
+----+----------------+-----------+-----------+
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 1 |
| 3 | 3 | 1 | 1 |
+----+----------------+-----------+-----------+
artist
+----+-------------+
| ID | ARTIST |
+----+-------------+
| 1 | Linkin Park |
| 2 | Metallica |
+----+-------------+
symbol
+----+--------+
| ID | SYMBOL |
+----+--------+
| 1 | |
| 2 | Feat. |
+----+--------+
tracklisting
+----+----------+----------+---------------+---------+
| ID | TRACK NO | TITLE_ID | VERSION | DISC NO |
+----+----------+----------+---------------+---------+
| 1 | 1 | 1 | | 1 |
| 2 | 1 | 2 | Album Version | 1 |
| 3 | 1 | 3 | Live Version | 1 |
+----+----------+----------+---------------+---------+
This is the final view I'm looking for
+----+----------+------------------+---------------+-----------------------------+---------+
| ID | TRACK NO | TITLE | VERSION | ARTIST | DISC NO |
+----+----------+------------------+---------------+-----------------------------+---------+
| 1 | 1 | Title Here | | Linkin Park Feat. Metallica | 1 |
| 2 | 1 | Another Title | Album Version | | 1 |
| 3 | 1 | Some Other Title | Live Version | Linkin Park | 1 |
+----+----------+------------------+---------------+-----------------------------+---------+
I have been bashing my head for the past 3 days with left, right, join and full join and just can't seem to get this to work.
Basically what I want to happen is the track_artist table will get the artist and symbol form the respective tables and concat them together into one column. Then join title and the concat column to have this view.
full_artist_view
+----------+------------------+-----------------------------+
| TITLE_ID | TITLE | FULL_ARTIST |
+----------+------------------+-----------------------------+
| 1 | Title Here | Linkin Park Feat. Metallica |
| 2 | Another Title | |
| 3 | Some Other Title | Linkin Park |
+----------+------------------+-----------------------------+
I have gotten this far but when I try join this to the tracklisting table I seem to crash my server which is getting very painful. No mysql error so I'm guessing I'm using the wrong join or this is just not possible.(though I can't see how this is not possible)
The tracklisting table is continuesly growing every week by 1000 records comfortable and is sitting at +- 75000 records.
To me this is the sql that should work, but doesn't
FROM full_artist_view LEFT JOIN tracklisting ON
full_artist_view.TITLE_ID = tracklisting.TITLE_ID
While I have only your small data sample and I can't see what your full_artist_view code looks like. You should be able to get the result using the following:
select tt.id,
tl.`track no`,
tt.title,
coalesce(tl.version, '') version,
group_concat(concat(coalesce(a.artist, ''), ' ', coalesce(s.symbol, '')) order by a.artist SEPARATOR ' ') artist,
tl.`disc no`
from track_title tt
inner join tracklisting tl
on tt.id = tl.TITLE_ID
left join track_artist ta
on tt.id = ta.TRACK_TITLE_ID
left join artist a
on ta.artist_id = a.id
left join symbol s
on ta.symbol_id = s.id
group by tt.id, tl.`track no`, tt.title, tl.version, tl.`disc no`
See SQL Fiddle with Demo. This returns:
| ID | TRACK NO | TITLE | VERSION | ARTIST | DISC NO |
---------------------------------------------------------------------------------------------
| 1 | 1 | Title Here | | Linkin Park Feat. Metallica | 1 |
| 2 | 1 | Another Title | Album Version | | 1 |
| 3 | 1 | Some Other Title | Live Version | Linkin Park | 1 |
Quick play, and I think (but not certain) it is something like this that you need:-
SELECT a.Id, a.Track_No, b.Title, a.Version, GROUP_CONCAT(CONCAT(d.Artists, e.Symbol)), a.Disc_No
FROM tracklisting a
INNER JOIN track_title b ON a.Title_id = b.Id
LEFT OUTER JOIN track_artist c ON b.Id = c.Track_Title_Id
LEFT OUTER JOIN artist d ON c.Artist_Id = d.Id
LEFT OUTER JOIN symbol e ON d.SymbolId = e.Id
GROUP BY a.Id, a.Track_No, b.Title, a.Version, a.Disc_No
But I might have missed something so let me know!
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