I have four db table,
tableA, tableB, tableC, tableD
I am listing records with join. These all working fine.
My query is like following.
Select DISTINCT A.*, B.loginname, B.email, C.full_name, C.address1, C.state, C.city, D.*
FROM (
tableA AS A
INNER JOIN tableB AS B ON B.user_id = A.id
INNER JOIN tableC AS C ON C.user_id = A.id
INNER JOIN tableD AS D ON D.user_id = A.id
) WHERE A.id = '269' ORDER BY A.created_date DESC
Now, issue is i have created four another table with almost same details but there are different fields and columns. So i can not use UNION.
tableE, tableF, tableG, tableH
i want to merge output to display. And output should be ORDER BY A.created_date OR ORDER BY E.created_date
Advanced thanks...
You can use UNION ALL (or UNION, if you want duplicates removed) between two tables with differently named columns by declaring name aliases.
SELECT 'first' AS source, a,b,c,d
FROM first
UNION ALL
SELECT 'second' AS source, q AS a, r AS b, s AS c, t AS d
FROM second
This will yield a result set (virtual table) with columns named source,a,b,c,d.
Related
I have a query which joins other tables and insert a custom row named action with static value started. At a later stage I will have to UNION 2 more querys which contains another static value in the row custom.
How do I implement this without having an own table for this values? Im used to PDO where I can make an array with my entrys and loop through. All i see in the documentation is where you have a fix table with these records.
select a.StartTimestamp, b.Name, b.LastName, 'Started' AS 'action', c.translation
from a
inner join b on a.ID = b.ID
inner join c on b.ID = c.ID
group by a.StartTimestamp
order by a.StartTimestamp DESC
limit 5;
My question pertains to joining data based on a specific condition.
For example, in table A there is a column known as user_type. If user_type is 1, I want to do an INNER JOIN from table B. However, if user_type is 2 in Table A, I want to do an INNER JOIN from table C.
Much appreciated if someone could iron out the syntax in PHP :-)
Use INNER JOIN with both tables as usual, and use a CASE statement just to show table B or table C result.
SELECT CASE WHEN tableA.user_type = 1 THEN tableB.user_type
WHEN tableA.user_type = 2 THEN tableC.user_type
END as user_type
FROM tableA
INNER JOIN tableB
ON tableA.id = tableB.id
INNER JOIN tableC
ON tableA.id = tableC.id
I have a database from which I need to JOIN 4 tables and I am having some trouble figuring it out
I have the following tables
A. id, name, picture, d_id
B. id, name
C. id, a_id, b_id, commentaar
D. id, name
Does anyone have any idea how to go about this? A has data used in D and C has data used in A and B, so I am at a loss how to get this one done. I want to output
A.name, A.picture, B.Name, C.commentaar, D.name
Unless I'm missing something, you just need to do this:
Select A.name, A.picture, B.Name, C.commentaar, D.name
From A
Join D On A.d_id = D.id
Join C On C.a_id = A.id
Join B On C.b_id = B.id
I am using a left to update values in multiple table . I have three table a b and c . i have a common id for all three tables. But i need to update all the tables in one go even if one of the tables does not have a entry ? is it possible. which join do i use ?
You could use multiple LEFT JOINs with a GROUP BY:
SELECT *
FROM a
LEFT JOIN b ON a.id = b.id
LEFT JOIN c ON a.id = c.id
GROUP BY a.id
Ok so I know we can join tables 'across' in MySQL to tables using a id column.
I have two databases where I need to join two tables that are almost identical.
I need to join these 'down', e.g similar to inserting the whole of one table into the other. Not a normal cross join.
Does MySQL allow me to do this?
UNION is a good way to accomplish this.
SELECT * FROM table1
UNION
SELECT * FROM table2
Just make sure number of columns being returned are the same, and data type matches.
Yes, You can use UNION - the only condition is to select the same amount of columns from both select quesries (both tables):
SELECT a, b, c, d FROM table1
UNION
SELECT d, e, f, g FROM table2
Common issue is to order the results from UNION, so here is solution:
SELECT a AS order_by, b, c, d FROM table1
UNION
SELECT d, e, f AS order_by, g FROM table2
ORDER BY order_by DESC
Try this :
INSERT INTO database1.table1
SELECT * FROM database2.table2
Yes you can do that, just use full database name before table, make sure your user have permissions on both databases.
SELECT ...
FROM database1.table t1
JOIN database2.table2 t2 ON t2.column = t1.col