MYSQL - Concatenate two tables
select * from table_a where actid = 17
union all
select * from table_b where actid = 17
You may (or may not) need to do something about the ids not being unique, such as
select 'Student', table_a.* from table_a where actid = 17
union all
select 'Faculty', table_b.* from table_b where actid = 17
How to join two tables of mysql and concatenate multiple rows into single cell?
Group concat is the easiest way to achieve the output you require.
Concatenate two tables in MySQL
JOIN
the two tables:
SELECT
t1.ID,
t1.Name,
t1.Age,
t2.Occupation,
t2.Address
FROM table1 t1
INNER JOIN table2 t2 ON t1.ID = t2.ID;
SQL Fiddle Demo
This will give you:
| ID | NAME | AGE | OCCUPATION | ADDRESS |
-----------------------------------------------
| 0 | John | 15 | Bus Driver | None |
| 1 | Chris | 20 | Lawyer | some adress |
If you want to create a new table table3
directly from this select, do this:
CREATE Table Table3
AS
SELECT
t1.Name,
t1.Age,
t2.Occupation,
t2.Address
FROM table1 t1
INNER JOIN table2 t2 ON t1.ID = t2.ID;
Like this
How to simply concatenate multiple tables horizontally in MySQL?
SELECT
a.ID1, b.ID2, a.x, b.x, c.x, < and so on >
FROM
a JOIN b ON a.ID1=b.ID1 AND a.ID2=b.ID2
JOIN c ON a.ID1=c.ID1 AND a.ID2=c.ID2
MySQL merge two tables and get sum
So, instead of JOIN
what you need is UNION
. You can use "UNION ALL
" or "UNION
", it depends if you want the duplicated rows or not.
In any case, after the UNION
, group that result into a subquery to get the SUM()
SELECT
u.name,
u.code,
SUM(u.num),
FROM
(
SELECT name, code, num FROM tableA
UNION ALL
SELECT name, code, num FROM tableB
) u
GROUP BY u.name, u.code
MySQL join two tables based on a condition
The conditions should be moved to the ON
clause.
In MySql a CASE
expression can be used to return the result of Boolean expressions (as 1
or 0
for true
or false
) like this:
SELECT u.name, s.swp_to, s.first_swp
FROM swipes s LEFT JOIN users u
ON CASE (s.swp_from = :me AND ((s.first_swp NOT IN ('like', 'superlike')) OR (s.first_swp IN ('like', 'superlike') AND s.second_swp NOT IN ('like', 'superlike'))))
WHEN 1 THEN u.id = s.swp_to
WHEN 0 THEN u.id = s.swp_from AND (s.swp_to = :me AND s.second_swp <> 'pending')
END;
Related Topics
Postgres Won't Accept Table Alias Before Column Name
Cascade on Delete or Use Triggers
How to Check the Query Is Using Index
Global Variables in SQL Statement
How to Sum Multiple Lines in SQL
SQL Difference Between in and or in Where
SQL Count of Open Orders Each Day Between Two Dates
Oracle Ora-00979 - "Not a Group by Expression"
How to Replace Multiple Characters in Access SQL
How to Select First N Rows from a Table in T-Sql
How to Insert Random Values into a SQL Server Table
Insert into Values with Where Clause
Date Calculation with Parameter in Ssis Is Not Giving the Correct Result
Ms Access 2010 Running Total in Query
Insert Deleted Values into a Table Before Delete with a Delete Trigger