How to write SQL query for this Scenario? - mysql

An interviewer asked me this question,
You are given two tables with columns as L1,T1 for table1 while T,Name for table2. Write a SQL query to get the desired result.
I'm still stuck on how to write the query to get the desired output.
Used this, SELECT table1.L1, table2.Name FROM table1 INNER JOIN table2 on table1.T1 = table2.T; but this way it won't CONCAT the output for Name wrt L1.
Thanks for helping in advance.

You need an aggegation function as GROUP_CONCAT for obtain the comma separated result for name
SELECT table1.L1,GROUP_CONCAT( table2.Name ORDER BY table2.Name ASC SEPARATOR ',')
FROM table1
INNER JOIN table2 on table1.T1 = table2.T
GROUP BY table1.L1
ORDER BY FIELD(table1.L1,'X','Y','Z')
withou aggegation function you get result on separated rows .. instead with group by an group_concat you obtain all the name related to a one l1 on the same row

You join the two tables using JOIN (Inner Join). Then use GROUP BY to get aggregated data, and then eventually utilize GROUP_CONCAT function to achieve comma separated names. See below:
SELECT table1.l1, GROUP_CONCAT(table2.name)
FROM table1
JOIN table2 ON table1.t1 = table2.t
GROUP BY table1.l1

use this query
SELECT table1.L1, GROUP_CONCAT(table2.name separator ', ')
FROM table1 AS table1
INNER JOIN table2 as table2 ON table1.T1 = table2.t
GROUP BY table1.L1
SQL fiddle here to test it live http://sqlfiddle.com/#!9/3ac3a3/1/0

This sql will get the result.
SELECT L1, GROUP_CONCAT(name) from Table1 INNER JOIN Table2 on T1 = T GROUP BY L1

Related

Mysql select from two tables where first table field = second table field%

I have two tables.
table1 has a row id='12345'
table2 has a row where the id = that of table1 with added chars, e.g.,
table2.id = '12345-678qt'
table2 may have more than one id starting with '12345-' with different ending chars.
Yes, there is always a dash after table1's id that could be used in the query.
I need to get some data from both tables, say
SELECT table1.id, table1.field9,
table2.id, table2.fieldZ
FROM table1 and table2
WHERE (
table1.id=table2.id's characters before the dash
OR
table1.field1 = 'abcde'
)
AND table2.dataB='something'
ORDER BY table1.datefield DESC LIMIT 3;
Thank you.
The MySQL translation of:
"table1 and table2 WHERE" is table1 INNER JOIN table 2 ON
"table2.id's characters before the dash" is SUBSTRING_INDEX(table2.id, '-', 1)
The query should look like this:
SELECT table1.id, table1.field9,
table2.id, table2.fieldZ
FROM table1
INNER JOIN table2
ON (table1.id = SUBSTRING(table2.id, '-', 1) OR table1.field1 = 'abcde')
AND table2.dataB = 'something'
ORDER BY table1.datefield DESC
LIMIT 3;
This doesn't grant that your query will work if it's written like this. My fixes will remove the current errors inside your query. For more troubleshooting with a sql fiddle link, please provide tables and I'll be happy to help.
Check the official documentation about the JOIN operation and the SUBSTRING_INDEX function at the corresponding links.

How to group only by joined table column with Doctrine?

I have one join table:
table1_id|table2_id
1|1
1|2
2|1
2|2
3|1
I need to select rows grouped by table2. But I need special grouping. In result row I want to see the following:
table1.id|grouped
1,2|1,2
3|1
I tried different ways.
Select from table1 and group by table2.id. Fail - MySql required query to be grouped my primary table as well.
Select from table2 and group by table1.id and table2.id . Fail - it selects rows from table2 separately, I want them to be concatenated as I shown in the example.
Select from table1 and group by group_concat(table2.id). But MySql does not allow to group by aggregated functions.
My example query: http://sqlfiddle.com/#!9/934b64/4. I need almost the same, but so that it will one row with values:
1,2|1,2
3|1
You are almost there.
You need to use GROUP_CONCAT in SELECT clause, not in GROUP BY clause.
SELECT table1_id,GROUP_CONCAT(table2_id) as grouped
FROM table
GROUP BY table1_id;
See the result in SQL Fiddle
For your requirement, please try the below query:
select t1.id,t1.value,
group_concat(t2.id) as grouped_ids,
group_concat(t2.value) as grouped_values
from table1 t1
join joined_table jt on jt.id1 = t1.id
join table2 t2 on t2.id = jt.id2
group by t1.id,t1.value
Result in SQL Fiddle

SQL select data from multiple tables with subqueries (including data from inner join) Error: 1242

I want to display the ID from Table1 (TID) and the results of an inner join.
The following statement is not working.
Situation: Two Tables:
Table 1 PK:TID, FK: Table2_PID
Table2 PK: PID, Name
Among other data I want to display the Name of every PID in Table1 which is stored in Table2.
SELECT T.TID
,(Select P.Name
from mydb.Table2 P
inner join mydb.Table1 T
on P.PID=T.Table2_PID)
FROM mydb.Ticket T;
Result: Error Code 1242. Subquery returns more than 1 row
I do know that the result returns more than 1 row, but I want to show the Name of every PID in Table1 which is stored in Table2. So any ideas on how I can do that?
PS: I'm using mySQL and working with MySQL Workbench v6.3
You don't need use the inner joins for getting all the names of the ID. You can try the default join to achieve the result.
select t2.pid, t2.name from mydb.Table2 t2, mydb.Table1 t1 where t1.pid = t2.pid;
Hope this helps.
You must use join like this
select t1.TID,t2.Name from Table1 t1 left join Table2 t2 on t1.Table2_PID=t2.PID
Thanks for the response, but the question/problem still remains.
It wasn't about the join.
It is about the subquery and selecting multiple rows within it.
Thank you guys,
I was thinking of a solution way to complicated. I resolved it using a simple where statement.
SELECT T.TID, P.Name
FROM mydb.table1 T, mydb.table2 P
WHERE P.PID=T.table2_PID;

How to apply result of order by clause from subquery to main query

I have 2 tables which are linked by column named MID.
I want to fetch name from 1st table but the sequence is mentioned in 2nd table.
My query is as follows:
select name from table1 where MID in(select MID from table2 where CID="100" ORDER BY sequenceNum);
If i only run the query mentioned inside brackets then i get the data ordered by sequence.
But the above query is fetching the data from db as it is and not arranging it in sequence. What can be the problem?
I think this shoukld do the trick...
SELECT name FROM table1
INNER JOIN table2 ON Table2.MID = table1.MID AND CID="100"
ORDER BY
table2.sequenceNum
You want merge two tables and order results by merged table:
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID)
WHERE table2.CID = "100"
ORDER BY table2.sequenceNum;
or
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID AND table2.CID = "100")
ORDER BY table2.sequenceNum;
If you want get field from concrete table, use table prefix like table1.name

Group Concatenate results of a Derived Table

I have a query, a generalized version of which I've reproduced below:
SELECT TT.column
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date
I want to take the output of this query -- a single column output with multiple rows sorted by date -- and group concat it in another query as a derived table:
SELECT
T.column2,
GROUP_CONCAT(
SELECT TT.column
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date) AS concat_output
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date
However, this returns an error at the line of the GROUP_CONCAT command.
Thoughts on how to make this work?
EDIT: To give some more detail on why I wanted the derived table to work:
At the moment, without using GROUP_CONCAT, I get multiple rows that look like
a
a
b
b
a
a
c
c
d
a
If I try to GROUP_CONCAT as described by Mukesh's answer, using DISTINCT I get the following, for example, as a row: a, b, c, d when really I want a,b,a,c,d,a.
Thoughts?
Try this query
SELECT
T.column2,
GROUP_CONCAT(
DISTINCT TT.column
) AS concat_output
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date
More detail to refer this link
http://www.mysqltutorial.org/mysql-group_concat/