I have run into a 1-N situation. i.e 1 record in 1st table will have multiple records in second table, I just wanna fetch all data corresponding to the ID in 1st table using joins, is this possible.
table 1 : ID name
1 pradeep
table 2: ID table1_id orders
1 1 23
2 1 25
3 1 26
In a single query i should get all records. I don't wanna loop as i am doing it and its taking lot of time.
I want to display user html output in manner like this. So there lies the problem.
ID Name orders1 orders2 orders order4
SELECT
*
FROM
table2
LEFT JOIN
table1 ON
table1.ID = table2.table1_id
SELECT
t2.* /* fetch data from second table */
FROM
table1 t1
LEFT JOIN table2 t2 on t2.table1_id = t1.id
WHERE t1.id = <id>;
I wouldn't recomment it though. Why don't you just run it as 2 separate queries?
Related
I'm building a small CRM, I want to know if there's a way to actually bring all rows from table 1 but also in the same result, each row's meta data from table 2, let's say tasks t2.
Right now I'm using 2 queries, one to bring all rows from t1, then in a loop I select all rows from t2 if there's a match on the t1_id I push to the existing array the data.
t1
-------------------
id
number
created_at
updated_at
t2
-------------------
id
t1_id
something
created_at
updated_at
This may solve you problem :
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.t1_id
I think the best is to use a group_concat but there is other ways :
SELECT t1.id,
t1.number,
t1.created_at,
t1.updated_at
GROUP_CONCAT(DISCTINCT t2.something) as meta
FROM t1
LEFT JOIN t2 on t1.id = t2.t1_id
GROUP BY t1.id,
t1.number,
t1.created_at,
t1.updated_at
I have 3 different tables in db those are like tbl1, tbl2, tbl3
in these all table have one same columns 'direction'.
tbl1 contain one value either tbl2 or tbl3,
tbl2 contains one value tbl3.
How can I select the rows from tbl1 and tbl2 which contains direction like equal tbl3.
i also include my two table image in this image 2 columns name is same level_redir now i want which row that have value like hardening
enter image description hereenter image description here
please suggest me which query is good for getting row from two different tables in which table columns where dir_redirect = 'hardening'.
Since your question is not clear and you have not included all the table structure its bit difficult to figure out.
Let me help you with generic way
The following query we have joined all the 3 tables based on the common tables with are present in the respective tables. The following query select all the columns from all the 3 tables.
SELECT * FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
You can select from specific tables by the following
SELECT t1.col1, t1.col2, t2.col1, t2.col2 FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
I have two tables, table1 and table2. table1 has 2 rows. table2 has 3 rows. so, totally table1 and table2 have 5 rows. I want to show the 5 rows by selecting table1 and table2 at a time. how todo? can you help me please. don't add any where clause.
depends on what the table structure is. if you have a PK > FK relationship you can join the tables like so
SELECT stuff
FROM table1 t1
JOIN table2 t2 ON t1.someID = t2.someID
if there is no correlation then you can use a UNION
SELECT stuff
FROM table1
UNION
SELECT stuff
FROM table2
One thing to note about using a UNION. the columns have to match so if you have the same type of data in both tables this works fine or else you will have to specify which columns to be selected out.
I want to merge the value records as single row based on ID value.I want to display table1 value along with table2 value as single row. I want to display all in single record for the id=15.In table 1 ID is sub_id in table 2.
I want output as
Id content_value value as(15,cake,chocolate,enila,strabery)
select a.*,b.CONTENT_VALUE as multitype from album as a,album_details as b where a.ID=b.SUB_ID
Not possible to get as different columns for each value or content_value since the number of rows is dynamic.You can get the values like this by joining two tables. ie as two columns (ID and all content_value & value).
SELECT t1.ID, CONCAT_WS(',', t1.content_value, GROUP_CONCAT(t2.value)) AS contents FROM table1 t1
INNER JOIN table2 t2
ON t2.sub_id = t1.ID
GROUP BY t1.ID;
Ok, I'll try to explain this as best I can here.
I have multiple tables that are to be connected through a JOIN where certain reference points meet.
In one of the tables, there are 2 or more results over several rows that I need to bring back to separate columns.
In the diagram below (I hope that explains it better), T2.ColA is connected to T3.ColA and T1,ColA is connected to T2.ColB.
In ColC of T1, there is a latest record. These are the only ones that are required to results. Note that ColC could be different dates between rows 1 and 2 for example. But it needs the latest for each ColB based on ColA.
But in T1, there are two rows which need Col B to return to the result in separate columns.
By the way, this is just one entry - there will be thousands of rows that need to return a result - not just 1.
Let me know if you need any more info.
Try this query
select t1.cola,t2.cola,t1.colb,t2.colb,t3.colb FROM table2 t2 INNER JOIN table1 t1 ON t1.cola = t2.colb INNER JOIN table3 ON t3.cola =t2.cola
Possibly this could solve your query..
SELECT T1.COLA, T1.COLB, T2.COLA, T2.COLA
FROM
TABLE1 T1 INNER JOIN TABLE2 T2
INNER JOIN TABLE3 T3
WHERE
T1.COLC = (
SELECT MAX(T4.COLC) FROM TABLE1 T4 WHERE T4.COLA = T1.COLA
)
** But as you have specified in your dig, it's not possible to display 1123, 3211 in a single row. It has to be in two different rows, coz its changes dynamically depending on the row count. You can modify as you want using your front end application.