I have the following 4 tables:
student_info:
S_ID naam email telefoon locatie U_ID
1 Walter Walter# 03938 Home 1
student_combi:
S_ID V_ID
1 1
student_vak:
V_ID vak R_ID
1 HTML 1
student_richting:
R_ID richting
1 Web-Development
I would like the query to SELECT 'vak' (from the 'student_vak' table) and 'richting' (from the 'student_richting' table) using INNER JOIN
So far I have this:
SELECT student_vak.vak,student_richting.richting
FROM student_vak
INNER JOIN student_richting ON student_vak.R_ID = student_richting.R_ID
INNER JOIN student_combi ON student_info.S_ID = student_combi.S_ID
INNER JOIN student_vak ON student_combi.V_ID = student_vak.V_ID
INNER JOIN student_richting ON student_vak.R_ID = student_richting.R_ID
Thank you in advance if you know the answer.
This following will fulfil your need.
SELECT S.S_Id Student_Id, S.Naam Student_Name, SV.vak, SR.richting
FROM Student_Info S
INNER JOIN Student_Combi SC ON S.S_id = SC.S_Id
INNER JOIN Student_Vak SV ON SC.V_Id = SV.V_Id
INNER JOIN student_richting SR ON SV.R_Id = SR.R_Id
--Example
CREATE TABLE #student_vak
(V_ID INT,
vak NVARCHAR(256),
R_ID INT)
INSERT INTO #student_vak
SELECT '1', 'HTML','1'
CREATE TABLE #student_richting
(R_ID INT,
richting NVARCHAR(256)
)
INSERT INTO #student_richting
SELECT '1', 'Web-Development'
SELECT SV.vak, SR.richting
FROM #student_vak SV
INNER JOIN #student_richting SR ON SV.R_Id = SR.R_Id
Related
I have bd hf3 and 5 tables there:
active_preset with columns (id , preset_id)
preset with columns (id , birja_id, trend_id, fractal, interval_up)
birja with columns (id , name)
trend with columns (id , name)
uq_active_preset with columns (id , birja, trend, fractal, interval_up)
In table preset I have a few records. Some of them are in table active_preset by foreign key preset_id. In table active_preset a few records exist once , a few more than once.
I need to update table uq_active_preset with records from table active_preset disregarding repetitions of records if they are present.
I did query from active_preset and it works good:
SELECT
b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
FROM hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
But I don't know how to update uq_active_preset
I tried this and it returns syntax error:1064 :
UPDATE hf3.uq_active_preset uap SET
uap.birja = st.birja ,
uap.fractal = st.fractal,
uap.trend = st.trend,
uap.interval_up = st.interval_up,
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
when you make an update using from is like you join the updated table with your query result. So, you need also a where statement in order to tell where those two are connected. Also, don't use alias of your updated table on set statement.
You need something like that:
UPDATE hf3.uq_active_preset uap SET birja=st.birja,fractal=st.fractal,trend=st.trend,interval_up=st.interval_up
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
where uap.fkey=st.fkey
I have two tables as follow:
table internetclient
(id,full_name,location,phone_number)
table internetclientdetails
(incdid,icid,date_sub, date_exp,isPaid,profile_sub)
the data in two table is as follow:
client
--------------------------------------------------------
id full_name location phone_number
-------------------------------------------------------
4 Joe Amine beirut 03776132
5 Mariam zoue beirut 03556133
client_subscription
--------------------------------------------------------------------------
incdid icid date_sub date_exp isPaid sub_price
----------------------------------------------------------------------------
6 4 2018-01-01 2018-01-30 0 2000
7 5 2017-01-01 2017-01-30 0 1000
8 4 2018-03-01 2018-03-30 1 50000
9 5 2018-05-01 2019-05-30 1 90000
note : incdid stands for internetClientDetailsId
and icid stands for internetClientId
Problem
I want to make a query that return client name along with all details depending on the latest client subscription date, the result should be as follow:
------------------------------------------------------------
full_name client_id date_sub sub_price
------------------------------------------------------------
Joe Amine 4 2018-03-01 50000
Mary 5 2018-05-01 90000
What i am tring
SELECT * FROM client c LEFT JOIN client_subscription c_s on c.id=c_s.client_id
UNION
SELECT * FROM client c RIGHT JOIN client_subscription c_S on c.id=c_s.client_id
WHERE
c.sub_date=(SELECT MAX(sub_date) from client_subscription c_s INNER JOIN client c on c.id=c_s.client_id GROUP BY c_s.client_id
i have been working on it all the night. Any help is appreciated a lot.
To get client_subscription for each client you could use a self join
select c.name, a.client_id, a.date_sub, a.sub_price
from client_subscription a
join (
select client_id, max(date_sub) date_sub
from client_subscription
group by client_id
) b on a.client_id = b.client_id and a.date_sub = b.date_sub
join client c on a.client_id = c.id
order by a.date_sub
Demo
Or using left join
select c.name, a.client_id, a.date_sub, a.sub_price
from client_subscription a
left join client_subscription b on a.client_id = b.client_id and a.date_sub < b.date_sub
join client c on a.client_id = c.id
where b.client_id is null
order by a.date_sub
Demo
Using your updated data set updated queries are
select c.full_name, a.icid, a.date_sub, a.sub_price
from internetclientdetails a
join (
select icid, max(date_sub) date_sub
from internetclientdetails
group by icid
) b on a.icid = b.icid and a.date_sub = b.date_sub
join internetclient c on a.icid = c.id
order by a.date_sub;
select c.full_name, a.icid, a.date_sub, a.sub_price
from internetclientdetails a
left join internetclientdetails b on a.icid = b.icid and a.date_sub < b.date_sub
join internetclient c on a.icid = c.id
where b.icid is null
order by a.date_sub
Updated Demo
Hi try below sample might help you.
DECLARE #tblClient AS TABLE (ID INT , Name varchar(100))
DECLARE #tblClientSub As TABLE (id INT,client_id INT,date_sub DATE,sub_price INT)
INSERT INTO #tblClient (id,Name)
VALUES
(1,'Linda'),
(2,'Mary'),
(3,'Joe')
INSERT INTO #tblClientSub(Id,client_id , date_sub , sub_price)
VALUES
(1,1,'2018/01/01',50),
(2,2,'2018/02/01',50),
(3,2,'2018/03/01',30),
(4,2,'2018/04/01',30),
(5,3,'2018/01/01',50),
(6,3,'2018/07/01',50),
(7,1,'2018/02/01',40)
SELECT c.Id,c.Name,cs.date_sub,cs.sub_price
FROM #tblClient c
CROSS APPLY (SELECT TOP (1)date_sub,sub_price
FROM #tblClientSub
WHERE client_id = c.Id
ORDER BY date_sub DESC) cs
select c.name as 'client_name',cs.client_id,max(cs.sub_date) as 'date_sub',cs.sub_price from client c ,
client_subscription cs where cs.client_id=c.id group by cs.client_id,cs.sub_price;
Try this
SELECT c.Name, c.id , MAX(date_sub), sub_price FROM client c LEFT JOIN client_subscription c_s on c.id=c_s.client_id
GROUP BY c.id
ORDER BY c.id ASC
I have the code below but i want to arrange it by date_available desc and group it by store_id so that i will only return 1 value.
SELECT
product_to_store.store_id,
product.date_available,
product.product_id,
product_description.name,
product_to_category.category_id,
pcd3.name,
lvl2.parent_id,
pcd2.name,
CASE
WHEN lvl2.parent_id > 0 THEN lvl1.parent_id
ELSE 0
END,
pcd1.name,
product.quantity,
product_type.type
FROM product
inner join product_to_store on (product.product_id=product_to_store.product_id)
inner join product_description on (product.product_id = product_description.product_id)
inner join product_to_category on (product.product_id = product_to_category.product_id)
inner join product_type on (product.product_id = product_type.product_id)
inner join store on (product_to_store.store_id = store.store_id)
inner join product_category as lvl2 on (product_to_category.category_id = lvl2.category_id)
inner join product_category as lvl1 on (lvl1.category_id = lvl2.parent_id)
inner join product_category_description as pcd3 on (pcd3.category_id = lvl2.category_id)
inner join product_category_description as pcd2 on (pcd2.category_id = lvl2.parent_id)
inner join product_category_description as pcd1 on (pcd1.category_id = lvl1.parent_id)
Sample table:
store_id date_available name
1 2017-05-04 T1
1 0000-00-00 T2
2 2017-06-04 T3
3 0000-00-00 T4
2 2017-04-04 T5
3 0000-00-00 T6
Expected result:
store_id date_available name
1 2017-05-04 T1
2 2017-06-04 T3
3 0000-00-00 T4
How can i do that in mysql? Please help me thank you.
Use this as a hint:
-- Create some testing data
IF OBJECT_ID('tempdb..#product') IS NOT NULL DROP TABLE #product
CREATE TABLE #product(
[store_id] int
,[date_available] varchar(50)
,[name] varchar(50)
)
INSERT INTO #product VALUES
(1, '2017-05-04', 'T1')
,(1, '0000-00-00', 'T2')
,(2, '2017-06-04', 'T3')
,(3, '0000-00-00', 'T4')
,(2, '2017-04-04', 'T5')
,(3, '0000-00-00', 'T6')
-- Get result
SELECT p1.[store_id], p1.[date_available], MIN(p2.[name]) AS [name]
FROM (
SELECT [store_id], MAX([date_available]) AS [date_available] FROM #product
GROUP BY [store_id]
) p1
INNER JOIN #product p2
ON p1.[store_id] = p2.[store_id] AND p1.[date_available] = p2.[date_available]
GROUP BY p1.[store_id], p1.[date_available]
-- Destroy the temp table
DROP TABLE #product
Then in your long script, you may rewrite it to something like this:
SELECT product_to_store.store_id,
product.date_available,
product.product_id,
.
.
.
FROM product
INNER JOIN ...
INNER JOIN ...
INNER JOIN (
SELECT p1.[store_id], p1.[date_available], MIN(p2.[name]) AS [name]
FROM (
SELECT [store_id], MAX([date_available]) AS [date_available] FROM #product
GROUP BY [store_id]
) p1
INNER JOIN #product p2
ON p1.[store_id] = p2.[store_id] AND p1.[date_available] = p2.[date_available]
GROUP BY p1.[store_id], p1.[date_available]
) store ON (product_to_store.store_id = store.store_id)
INNER JOIN ...
INNER JOIN ...
I'm trying to write a query to sum all the prc values from different tables and UPDATE it to main_trans.tot.
ex. tot value for TR01 should be 30 adding all TR01 prcs from two tables.
Table main_trans:
| id | tot |
|TR01| 30 |
|TR02| 5 |
TABLE sub_trans_a:
| id | prc |
|TR01| 10 |
|TR01| 10 |
TABLE sub_trans_b:
| id | prc |
|TR01| 10 |
|TR02| 5 |
I don't know how to write it so that it would automatically update all rows based on their id. So far, my query does the work if i specifically write the value of the id column:
UPDATE main_trans SET tot =
(SELECT SUM(prc) FROM sub_trans_a WHERE id = 'TR01')
+ (SELECT SUM(prc) FROM sub_trans_b WHERE id = 'TR01')
WHERE id = 'TR01'
You can use a join in your update query with a union set
UPDATE main_trans m
join
(SELECT id,SUM(prc) prc
FROM (
SELECT id,SUM(prc) prc FROM sub_trans_a WHERE id = 'TR01'
union all
SELECT id,SUM(prc) prc FROM sub_trans_b WHERE id = 'TR01'
) t1
) t
on(t.id = m.id)
SET m.tot = t.prc
WHERE m.id = 'TR01'
Also if you have same structure for sub_trans_a and sub_trans_a so why 2 tables why not just a single table or with a single column for the type as type a or type b
See Demo
Or if you want to update your whole main_trans table without providing id values you can do so by adding a group by in query
UPDATE main_trans m
join
(SELECT id,SUM(prc) prc
FROM (
SELECT id,SUM(prc) prc FROM sub_trans_a group by id
union all
SELECT id,SUM(prc) prc FROM sub_trans_b group by id
) t1 group by id
) t
on(t.id = m.id)
SET m.tot = t.prc
See Demo 2
Edit a good suggestion by Andomar you can simplify inner query as
UPDATE main_trans m
join
(SELECT id,SUM(prc) prc
FROM (
SELECT id,prc FROM sub_trans_a
union all
SELECT id,prc FROM sub_trans_b
) t1 WHERE id = 'TR01'
) t
on(t.id = m.id)
SET m.tot = t.prc
WHERE m.id = 'TR01'
If you want to do the update for all at the same time, just use correlated subqueries:
UPDATE main_trans mt
SET tot = ( (SELECT SUM(prc) FROM sub_trans_a a WHERE a.id = mt.id) +
(SELECT SUM(prc) FROM sub_trans_b b WHERE b.id = mt.id)
);
If one or both tables may not have values, then the result might be NULL. You can fix this using COALESCE():
UPDATE main_trans mt
SET tot = ( COALESCE((SELECT SUM(prc) FROM sub_trans_a a WHERE a.id = mt.id), 0) +
COALESCE((SELECT SUM(prc) FROM sub_trans_b b WHERE b.id = mt.id), 0)
);
I want to fetch record from mysql database on following condition
I have the following tables:
organization_ Table
organizationId | organizationname
campaign Table
camp_id | camp_name | adv_id | organizationId
advertise Table
adv_id | adv_name
ad_display Table
ad_displayId | adv_id | camp_id |
Now from ad_display table I want to fetch record like this:
| adv_name | camp_name | organizationname
With WHERE Condtion as Where organizationId == ?
So basically I want the advertise list of campaign where organization_id=?
Note: there should be multiple entry for same adv_id in camapign table and same camp_id with multiple entry in ad_display
Try this:
SELECT ad.ad_displayId, a.adv_name, c.camp_name, o.organizationname
FROM ad_display ad
INNER JOIN advertise a ON ad.adv_id = a.adv_id
INNER JOIN campaign c ON ad.camp_id = c.camp_id
INNER JOIN organization o ON c.organizationId = o.organizationId
WHERE o.organizationId = ?;
Maybe something like this?
SELECT Distinct
advertise.adv_name,
campaign.camp_name,
organization_.organizationname
FROM
advertise inner join campaign
on advertise.adv_id = campaign.adv_id
inner join organization_
on campaign.organizationID = organization_.organizationID
WHERE
organization_.organizationID = 1
SELECT advertise.adv_name,
campaign.camp_name,
organization_Table.organizationname
FROM ad_display
INNER JOIN advertise ON ad_display.adv_id = advertise.adv_id
INNER JOIN campaign ON ad_display.camp_id = campaign.camp_id
INNER JOIN organization_ Table ON campaign.organizationId = organization_Table.organizationId
AND organization_Table.organizationId=?
SELECT * FROM campaign
LEFT JOIN ad_display ON campaign.camp_id = ad_display.camp_id
LEFT JOIN advertise ON ad_display.adv_id = advertise.adv_id
WHERE campaign.organizationId = <?>
Use below code
SELECT dv_name,
camp_name,
organizationname
FROM organization
inner join campaign
ON organization.organizationid == campaign.organizationid
left join advertise
ON campaign.adv_id == advertise.adv_id
WHERE condition =? ;
SELECT batch_number,
Count(receipt_number) AS tot_rec,
Sum(amount) AS tot_amount
FROM receipt_header
GROUP BY batch_number
UNION ALL
SELECT batch_number,
Count(receipt_lines.receipt_number),
Sum(receipt_lines.amount)
FROM receipt_lines
INNER JOIN receipt_header
ON receipt_header.record_id = receipt_lines.header_record_id
GROUP BY batch_number
ORDER BY batch_number ASC;