Unioning two tables with different number of columns
Add extra columns as null for the table having less columns like
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
How do I Union two tables with different columns
I suspect you want aggregation:
SELECT MonthId, DepartmentId, Salary, Deduction, Paid, MAX(Bank_1), MAX(Bank_2)
FROM (SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Null AS Bank_1, Null AS Bank_2
FROM Table_A
WHERE MonthId = '02/2016'
UNION ALL
SELECT MonthId, DepartmentId, Salary, Deduction, Paid, Bank_1, Bank_2
FROM Table_B
WHERE MonthId = '02/2016'
) ab
GROUP BY MonthId, DepartmentId, Salary, Deduction, Paid;
UNION 2 Table with different Column name and different number of columns
Change your statement, so that you have the same number (and type) in both select statements:
$result = $db->rows("SELECT calendar_id, calendar_title, calendar_starttime, calendar_endtime
FROM (SELECT academic_schedule_id AS calendar_id, academic_schedule_name AS calendar_title, academic_schedule_datetimestart AS calendar_starttime, academic_schedule_datetimeend AS calendar_endtime
FROM academic_schedule_table
WHERE academic_schedule_faculty = 'Teknik Mesin' AND academic_schedule_status = 'Active'
UNION ALL
SELECT appointment_table.dosen_schedule_id AS calendar_id, dosen_schedule_table.dosen_schedule_name AS calendar_title, dosen_schedule_table.dosen_schedule_datetimestart AS calendar_starttime, dosen_schedule_table.dosen_schedule_datetimeend AS calendar_endtime
FROM appointment_table
INNER JOIN dosen_table
ON dosen_table.dosen_id = appointment_table.dosen_id
INNER JOIN dosen_schedule_table
ON dosen_schedule_table.dosen_schedule_id = appointment_table.dosen_schedule_id
WHERE appointment_table.siswa_id = ".$_SESSION['siswa_id']." AND appointment_table.status = 'Disetujui'
) ab
GROUP BY calendar_id, calendar_title, calendar_starttime, calendar_endtime");
SQL Server : how to union 3 tables with different number of rows
Number of rows doesn't matter for a union number of columns do.
What do you expect with a union?
I suppose you want you want all information (name address, contact) of customer having Id= @CustomerId
If I am right, you should make 2 join between the 3 tables using CustomerId as join clause
Edit:
https://www.db-fiddle.com/f/uGhtAZAVk64KzgThGekKk6/11
Union with different number of columns
Just add null
or any other default value you like as static column
select c1 as col1,
c2 as col2,
c3 as col3,
null as col4,
null as col5
from Table1
union
select c1,
null,
null,
c4,
c5
from Table2
SQL UNION two tables with different column names
The logic in your query looks fine. I question the back ticks and the parentheses are not needed. Does this do what you need?
CREATE VIEW Dataset.Combined_Table AS
SELECT Date, Account, Measure
FROM Dataset.Table1
UNION ALL
SELECT Date, Account, Metric as Measure
FROM Dataset.Table2;
SQL Server : Union two tables with different columns
You need full outer join
:
select coalesce(t1.name, t2.name), coalesce(t1.Region, t2.Region),
t1.Price_2018, t1.Cost_2018, t2.Price_2017, t2.Cost_2017
from table1 t1 full outer join
table2 t2
on t2.name = t1.name;
Related Topics
Auto Increment the Column Value by 1 Conditionally in Select Query in SQL
Select and Count in One Query from Two Tables With MySQL
Mysql Split Comma Separated String into Temp Table
Sql Select Count for Multiple Columns in a Single Query
Count Number of Unique Characters in a String
Oracle SQL Get the First and Last Records from an Ordered Dataset
How to Select Only 1 Row from Oracle SQL
Comparing Two Count Results Using SQL
Group MySQL Query by 15 Min Intervals
Mysql: Error Code: 1054. Unknown Column in 'Where Clause'
How to Find Multiple Occurrence of Particular String and Fetch Value in SQL Server
How to Get the Month and Day With Leading 0'S in SQL (E.G. 9 => 09)
Sql Select Join: How to Prefix All Columns as 'Prefix.*'
Delete Rows With Date Older Than 30 Days With SQL Server Query
How to Change Column Order in a Table Using SQL Query in SQL Server 2005
Unable to Get Min and Max Time from a Datetime Column from Every Backday
What Is the Best Datatype for Storing Urls in a MySQL Database
Sqlstate[Hy000] [1045] Access Denied for User 'Root'@'Localhost' (Using Password: No)