how to merge two table with different columns in sql - mysql

I have two table named as booked_flat and master_flat.
columns of booked_flats are
customer_name, customer_address, customer_phone, building_wing_no, building_name_no, flat_no
columns of master_flats are
building_wing_no, building_name_no, flat_no, status
when any flat get booked the status get change as sold else status remains unsold.
all entries with sold status are in booked_flats.
I have tried to merge this two table but it gives an error please help me
$sql = SELECT * FROM 'booked_flats'
UNION SELECT * FROM 'master_flats'
WHERE building_wing_no, building_name_no, flat_no NOT IN (
SELECT building_wing_no, building_name_no, flat_no from booked_flats);
example:
**master_flat:**
building_wing_no building_name_no flat_no status
-----------------------------------------------
a a2 104 unsold
-------------------------------------------------
b a3 105 sold
booked_flat:
custo_name custo_add custo_ph building_wing_no building_name_no flat_no
---------------------------------------------------------------------------
harish wardha 284632 b a3 105
I want result like
cust_name cust_add custo_ph building_wing_no building_name_no flat_no status
harish wardha 284632 b a3 105
--- --- --- a a2 104 unsold

I think what you really want to do is a join and not an union, but you should update your question and write what you want to display.
SELECT * FROM booked_flats b
JOIN master_flats m ON b.building_wing_no = m.building_wing_no
AND b.building_name_no = m.building_name_no AND b.flat_no = m.flat_no;

Related

Merging two tables using SQL

I have two tables that I need to merge.
Table 1 is :
ID
Product code
Spend
1
101
100
1
102
200
1
103
300
2
201
400
3
301
500
3
302
600
Table 2 has
ID
Product code
Spend
Product tenure
1
101
100
20
1
102
200
30
3
302
600
40
I want to merge these such that only ID's present in table 2 are retained from table 1. Table 2 does not contain all the product codes for each ID, but I want my final table to have it.
Output must have
ID
Product code
Spend
Product tenure
1
101
100
20
1
102
200
30
1
103
300
3
301
500
3
302
600
40
Any help on this would be appreciated. I tried left join on ID, but it produces many duplicates.
SELECT *,
(
SELECT `product_tenure`
FROM `second_table`
WHERE `second_table`.`id` = `first_table`.`id`
AND `first_table`.`product_code` = `second_table`.`product_code`
) product_tenure
FROM `first_table`
WHERE `id` IN (SELECT DISTINCT `id` FROM `second_table`)
Explaination:
Select id from second table, which wanted to keep from first table.
Because the product_tenure only in second_table select them with combined id and product_code
Result:
Test this:
SELECT *
FROM t1
LEFT JOIN t2 AS t21 USING (ID, `Product code`)
WHERE EXISTS ( SELECT NULL
FROM t2 AS t22
WHERE t1.ID = t22.ID )
PS. The query will return 2 Spent columns (they're present in each table, and nothing prevents their values to be not equal...) - so replace an asterisk with definite columns list.

How to add automatic dummy row using sql

I have table with the following:
ID TYPE ProjectID Date Rev
1 A 1 30-1-2010 500
2 B 1 28-02-2011 580
2 B 2 30-04-2011 540
2 B 3 03-04-2019 440
Results:
ID TYPE ProjectID Date Rev
1 A 1 30-1-2010 500
1 A 2 01-01-2000 0
1 A 3 01-01-2000 0
2 B 1 28-02-2011 580
2 B 2 30-04-2011 540
2 B 3 03-04-2019 440
I want to write an SQL query in which, whenever there is Type “A”, two rows should automatically be inserted with project id 2 and 3 and with default Date and Rev data.
Currently, I am using UNION function to add this data manually, but I want to do it automatically.
I am not sure how to do this in SQL.
If I understand correctly:
select it.id, it.type, p.projectid,
coalesce(t.date, '2000-01-01') as date, coalesce(t.rev, 0) as rev
from (select distinct id, type from t) it cross join
(select distinct projectid from t) p left join
t
on t.id = it.id and t.type = it.type and t.projectid = p.projectid;
This fills in the missing values, so all id/type combinations have all projects.

Remove Duplicate Amounts

I have two tables with invoice detail and serial numbers.
Both tables have invoice number, but when I attempt to join using left or inner join the amount duplicates in each lines. I want to list down all the serials associated with my invoice # in table 1 without duplicating the amount each lines. I'm currently using MS Access.
Thanks for the help.
Table 1
Invoice# amount
001 500
Table 2
Invoice# serial
001 123
001 456
001 789
001 1011
001 1213
Desired Output:
Invoice# amount serial
001 500 123
456
789
1011
1213
My Current Query Output:
Query:
Select invoice.invoice,invoice.amount,tblmachine.serial
From tblmachine inner join invoice on tblmachine.invoice =invoice.invoice;
Use UNION ALL for 2 queries.
The 1st will return the 1st row that you need and the 2nd all the rest:
select i.[invoice#], i.amount, min(t.serial) as serial
from tblmachine as t inner join invoice as i
on t.[invoice#] = i.[invoice#]
where i.[invoice#] = '001'
group by i.[invoice#], i.amount
union all
select null, null, t.serial
from tblmachine as t
where t.[invoice#] = '001'
and t.serial > (select min(serial) from tblmachine where [invoice#] = t.[invoice#])
Results:
invoice# amount serial
001 500 123
456
789
1011
1213
Can't do that desired output in query for multiple invoices.
Build a report and set textbox HideDuplicates property to Yes.

Mysql join not showing all records

I am using Php with join, I have two tables ("services" and "service_detail"), I want to get all services
but want to know which service selected or notselected by vendor
Here is my services table strcture
id service_name image
1 Abc abc.jpg
2 xyz xyz.jpg
3 OPS ops.jpg
4 tys tys.jpg
5 byp byp.jpg
Here is my services_detail table strcutre
id vendor_id service_id price
1 101 1 50
2 101 2 70
3 101 3 80
4 101 4 30
5 102 1 70
6 102 2 40
...
I tried with following query but showing only selected services, but i want to get all services with parameter ( selected or notselected)
Where i am wrong ? Here is my query
SELECT sd.vendor_id, sd.service_id, sd.price, s.service_name, s.image
FROM services_detail sd
LEFT JOIN services s
ON sd.service_id = s.id
WHERE sd.vendor_id = '101'
Move your where clause to be AND in ON clause:
AND sd.vendor_id = '101'
And interchange tables in join to get all servcies
SELECT sd.vendor_id, sd.service_id, sd.price, s.service_name, s.image,
IF (sd.vendor_id is not null, 'Opted', 'Not opted') as status
FROM services s
LEFT JOIN services_detail sd
ON sd.service_id = s.id AND sd.vendor_id = '101';
In simple words, when there is a where clause including filters on table of Left Join then it will act like INNER JOIN not LEFT JOIN.

Join two tables using mysql

table:tab1
id date_time zoneid accountid slotid trequest bidder width height
_50832 2017-09-04 15:41:06 153 1654 153x468x60 10 aaa 468 60
_50832 2017-09-04 15:41:06 152 1654 152x468x60 10 bbb 468 60
table:tab2
id date_time zoneid accountid slotid bidder count
_50832 2017-09-04 15:41:06 152 1654 152x468x60 bbb 6
_50832 2017-09-04 15:41:06 152 1654 152x468x60 bbb 4
_50832 2017-09-04 15:41:06 153 1654 153x468x60 aaa 9
_50832 2017-09-04 15:41:06 153 1654 153x468x60 aaa 1
below is my query:
SELECT SUM(req.trequest) as REQ, SUM(win.count) as IMP
FROM tab1 as req
JOIN tab2 as win ON (req.id=win.id AND req.zoneid=win.zoneid)
GROUP BY req.zoneid
I get below result,
REQ IMP
20 10
20 10
IMP count is correct but I get wrong REQ count. My expected result is
REQ IMP
10 10
10 10
How to get my expected result?
Lets find the sum of trequest and count separately based on zoneid and id.Then use these two results ( t1 and t2 ) in the inner join.
Count mismatch problem shown in the question occur due to multiple rows satisfying the joining conditions.
In this solution we will only have one entry for each zoneid in both the results ( t1 and t2 ). So the problem is avoided.
Note: You can remove the id column from the GROUP BY clause if it doesn't make any difference.
SELECT t1.id, t1.zoneid, t1.REQ, t2.IMP FROM
(SELECT id,zoneid,SUM(trequest) as REQ
FROM tab1 GROUP BY zoneid,id ) t1
INNER JOIN
(SELECT id,zoneid SUM(win.count) as IMP
FROM tab2 GROUP BY zoneid,id ) t2
ON t1.id = t2.id
AND t1.zoneid = t2.zoneid
Let's try first sumwin.count and group records in sub-query, after it join tables. Try in following:
SELECT SUM(req.trequest) as REQ, SUM(win.count) as IMP
FROM tab1 as req
JOIN (
SELECT SUM(win.count) as IMP, win.zoneid, win.id
FROM tab2 as win
GROUP BY win.zoneid, win.id) AS win ON req.id=win.id AND req.zoneid=win.zoneid
GROUP BY req.zoneid
Instead of req.zoneid. You should try win.zoneid. What seems is that the rows in table 1 are counted multiple times as zoneid in table 2 comes twice. So win.zoneid would group it and avoid the repetition.
Updated: The solution posted by #mayur panchal is the correct one as you don't need to SUM the rows in first table as they belong to different zoneid. If you SUM them you will obviously get the 20 repeated twice.