I've three tables
students_first_semester_mark (StudentdID,Subject, Semester_I_Mark )
students_second_semester_mark (StudentdID,Subject, Semester_II_Mark )
students_third_semester_mark (StudentdID,Subject, Semester_III_Mark )
I want to have the following kind of output:
_________________________________________________________________________________
|StudentdID | Subject | Semester_I_Mark| Semester_II_Mark | Semester_III_Mark
_________________________________________________________________________________
Please note that Semester_I_Mark or Semester_II_Mark may have Null values.
I'm eager for your answers.
Is this what you want?
SELECT [students_first_semester_mark].[StudentdID]
,[students_first_semester_mark].[Subject]
,[students_first_semester_mark].[Semester_I_Mark]
,[students_second_semester_mark].[Semester_II_Mark]
,[students_third_semester_mark].[Semester_III_Mark]
FROM [students_first_semester_mark]
JOIN [students_second_semester_mark] ON [students_second_semester_mark].[StudentdID] = [students_first_semester_mark].[StudentdID]
JOIN [students_third_semester_mark] ON [students_third_semester_mark].[StudentdID] = [students_second_semester_mark].[StudentdID]
Below is the simplest answer that I can think of.
SELECT T1.StudentdID,T1.Subject,T1.Semester_I_Mark,
T2.Semester_II_Mark, T3.Semester_III_Mark
FROM students_first_semester_mark T1, students_second_semester_mark T2,
students_third_semester_mark T3
JOIN T2 ON T1.StudentdID = T2.StudentdID
JOIN T3 ON T1.StudentdID = T3.StudentdID
This might help you:
SELECT table1.StudentdID, table1.Subject,
table1.Semester_I_Mark, table2.Semester_II_Mark,
table3.Semester_III_Mark
FROM students_first_semester_mark table1
INNER JOIN students_second_semester_mark table2
ON table1.StudentdID = table2.StudentdID
INNER JOIN students_third_semester_mark table3
ON table1.StudentdID = table3.StudentdID
I am assuming that all students will not appear in all subjects.
It will be bit tricky to achieve it in MySQL as it doesn't support full outer join.
To derive FULL OUTER JOIN, you need a LEFT JOIN UNION RIGHT JOIN. As in your case, there are 3 tables, you would need to repeat this for first 2 tables and then UNION them. So the query will be pretty complex to look.
DBFiddle Demo
select * from (
select coalesce(fs.studentdid,t.studentdid) as STUDENTDID
,coalesce(fs.subject,t.subject) as subject
,fs.semester_i_mark,fs.semester_ii_mark,t.semester_iii_mark
from
(
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
left join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
UNION
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
right join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
) fs
LEFT JOIN
STUDENTS_THIRD_SEMESTER_MARK t
on fs.STUDENTDID=t.studentdid
and fs.subject=t.subject
) u1
UNION
select * from (
select coalesce(fs.studentdid,t.studentdid) as STUDENTDID
,coalesce(fs.subject,t.subject) as subject
,fs.semester_i_mark,fs.semester_ii_mark,t.semester_iii_mark
from
(
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
left join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
UNION
select
coalesce(f.studentdid,s.studentdid) as STUDENTDID,
coalesce(f.subject,s.subject) as subject
,f.semester_i_mark,s.semester_ii_mark
from
STUDENTS_FIRST_SEMESTER_MARK f
right join
STUDENTS_SECOND_SEMESTER_MARK s
on f.studentdid=s.studentdid
and f.subject=s.subject
) fs
RIGHT JOIN
STUDENTS_THIRD_SEMESTER_MARK t
on fs.STUDENTDID=t.studentdid
and fs.subject=t.subject
) u2
Coalesce is used so that any not null values of studentdid and subject are returned as we are using outer join.
Related
I need to get rows out from article and users table when the slug appears in my highlight table.
Highlight Table
id | slug
1 blue
2 green
Article Table
id | slug | title
1 blue
2 pink
User Table
id | slug | name
1 blue
2 green
3 brown
Heres my query:
SELECT slug from highlight_table
INNER JOIN article_table ON highlight_table.slug = article_table.slug
INNER JOIN user_table ON highlight_table.slug = user_table.slug
I would hope to get id 1 from article table and id 1 and 2 from users table.
The issue is Im getting nothing back from the query.
The query has an error because your SELECT slug is ambiguous. Your column slug appears in all of your tables so MySQL doesn't know which column to return. You need to do
SELECT `highlight_table`.`slug` from `highlight_table`
This will tell MySQL to only return the slug column from the highlight_table.
You should then only get 1 row which is blue, because blue exists in all three tables. Changing to LEFT JOIN for both article and user tables would get you 2 results back (green and blue) as INNER JOIN basically works as an AND and LEFT JOIN works more like an OR
Update!
Based on the final lot of information here is a query that does work:
SELECT highlight.slug from highlight
LEFT JOIN article ON highlight.slug = article.slug
LEFT JOIN user ON highlight.slug = user.slug
WHERE
article.slug IS NOT NULL OR user.slug IS NOT NULL
Another example of doing this:
SELECT `highlight`.`slug` from `highlight`
WHERE `highlight`.`slug` IN (SELECT `user`.`slug` FROM `user` UNION SELECT `article`.`slug` FROM `article`)
OR
SELECT `highlight`.`slug` from `highlight`
INNER JOIN (SELECT `user`.`slug` FROM `user` UNION SELECT `article`.`slug` FROM `article`) AS `allslugs` ON `highlight`.`slug` = `allslugs`.`slug`
Another update, I call this one "fun with joins"
SELECT `highlight`.`slug` from `highlight`
RIGHT JOIN `user` ON `highlight`.`slug` = `user`.`slug`
LEFT JOIN `article` ON `highlight`.`slug` = `article`.`slug`
WHERE
`highlight`.`slug` IS NOT NULL
Try changing your query to qualify the column name in select list
SELECT h.`slug` from HighlightTable h
INNER JOIN ArticleTable a ON h.`slug` = a.`slug`
INNER JOIN UserTable u ON h.`slug` = u.`slug`;
Can't reproduce the issue. See This Fiddle
per your latest comment you need a LEFT JOIN query like
SELECT h.`slug` from HighlightTable h
LEFT JOIN ArticleTable a ON h.`slug` = a.`slug`
LEFT JOIN UserTable u ON h.`slug` = u.`slug`;
Then do a separate JOIN and UNION the result set
SELECT h.`slug` from HighlightTable h
INNER JOIN ArticleTable a ON h.`slug` = a.`slug`
UNION
SELECT h.`slug` from HighlightTable h
INNER JOIN UserTable u ON h.`slug` = u.`slug`;
Hi and sorry if I don't do the question the correct way
I have this query :
SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas
FROM
act_usuario t1
INNER JOIN
act_regisdiario t2
ON
(t2.usu_id = t1.usu_id)
INNER JOIN
act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
act_tarea t4
ON
(t4.tar_id = t3.tar_id)
WHERE
t4.usu_id = 4
GROUP BY
t3.rea_id
ORDER BY
t2.rdi_fechacti
And the query print this :
So, I need when in "fecha_Actividad" exist the same "nombre_actividad" , thats only show me one "fecha_actividad" and the sum of "horas_trabajadas".
For example the query show this:
But I need this (because the same "nombre_tarea" is in the same date):
The reference:
Sorry my english.
You can simply use SUM(field) syntax after grouping.
SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas,
SUM(t3.rea_hrstarea)
FROM
act_usuario t1
INNER JOIN
act_regisdiario t2
ON
(t2.usu_id = t1.usu_id)
INNER JOIN
act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
act_tarea t4
ON
(t4.tar_id = t3.tar_id)
WHERE
t4.usu_id = 4
GROUP BY
t4.tar_nombre
ORDER BY
t2.rdi_fechacti
There are also other functions you can use on grouped results. You can find them here.
Is there a way that I can combine these two queries:
FIRST QUERY
select top 100
WORK.pzInsKey,
WORK.pyID,
PARTY.MacID,
PARTY.OtherPartyID,
PARTY.CustomerEmail,
ACCOUNT.AccountNumber,
ACCOUNT.AccountName,
ACCOUNT.AdviserCode,
ACCOUNT.AdviserName,
ACCOUNT.DealerCode,
ACCOUNT.DealerName,
ACCOUNT.PrimaryAccount,
ACCOUNT.ProductCategory,
ACCOUNT.ProductCode,
ACCOUNT.ProductDescription,
ACCOUNT.RegisteredState,
DOCUMENT.UDOCID
from
workTable WORK,
partyTable PARTY,
accountTable ACCOUNT,
documentTable DOCUMENT,
notesTable NOTES
where WORK.pzInsKey = PARTY.pxInsIndexedKey
and WORK.pzInsKey = ACCOUNT.pxInsIndexedKey
and WORK.pyID = DOCUMENT.CaseID
and SECOND QUERY
SELECT top 100
BusinessAreaTbl.businessarea,
ProcessTbl.process,
SubProcessTbl.subprocess
FROM workTable WORK
LEFT OUTER JOIN (SELECT DISTINCT Product_ID businessarea_id, Product businessarea from CaseTypesTable) BusinessAreaTbl
ON WORK.RequestBusinessArea#1 = BusinessAreaTbl.businessarea_id
LEFT OUTER JOIN (SELECT DISTINCT Process_ID, Process, Product_ID businessarea_id from CaseTypesTable) ProcessTbl
ON WORK.RequestProcess#1 = ProcessTbl.process_id
AND ProcessTbl.businessarea_id = WORK.RequestBusinessArea#1
LEFT OUTER JOIN (SELECT DISTINCT SubProcess_ID, SubProcess, Product_ID businessarea_id, Process_ID from CaseTypesTable) SubProcessTbl
ON WORK.RequestSubProcess#1 = SubProcessTbl.subprocess_id
AND SubProcessTbl.businessarea_id = WORK.RequestBusinessArea#1
AND SubProcessTbl.process_id = WORK.RequestProcess#1
It's basically two queries which produce separate results, but each query includes data from the workTable. In the 2nd query, the workTable data is derived from the CaseTypesTable.
I essentially just want the businessarea, process, and subprocess fields to be included with the results of the first query.
Thanks in advance for any help.
This should work:
(SELECT top 100
w.pzInsKey,
w.pyID,
p.MacID,
p.OtherPartyID,
p.CustomerEmail,
a.AccountNumber,
a.AccountName,
a.AdviserCode,
a.AdviserName,
a.DealerCode,
a.DealerName,
a.PrimaryAccount,
a.ProductCategory,
a.ProductCode,
a.ProductDescription,
a.RegisteredState,
d.UDOCID
FROM workTable w
LEFT JOIN partyTable p
ON w.pzInsKey = p.pxInsIndexedKey
LEFT JOIN accountTable a
ON w.pzInsKey = a.pxInsIndexedKey
LEFT JOIN documentTable d
ON w.pyID = d.CaseID)
UNION
(SELECT top 100
ba.businessarea,
pr.process,
spr.subprocess
FROM workTable w
LEFT OUTER JOIN (SELECT DISTINCT Product_ID businessarea_id, Product businessarea from CaseTypesTable) BusinessAreaTbl ba
ON w.RequestBusinessArea#1 = ba.businessarea_id
LEFT OUTER JOIN (SELECT DISTINCT Process_ID, Process, Product_ID businessarea_id from CaseTypesTable) ProcessTbl pr
ON w.RequestProcess#1 = pr.process_id
AND pr.businessarea_id = w.RequestBusinessArea#1
LEFT OUTER JOIN (SELECT DISTINCT SubProcess_ID, SubProcess, Product_ID businessarea_id, Process_ID from CaseTypesTable) SubProcessTbl spr
ON w.RequestSubProcess#1 = spr.subprocess_id
AND spr.businessarea_id = w.RequestBusinessArea#1
AND spr.process_id = w.RequestProcess#1))
Use the keyword UNION to combine two or more seperate SELECT statements.
I need to update multiple records in a table based upon the sum of some values in another table. Here is my query:
UPDATE aallinnot2 c SET c.Energ_Kcal = ( SELECT d.id1, SUM( c.Energ_Kcal)
FROM aaingred a
LEFT JOIN aaweight b ON a.unit = b.uni
LEFT JOIN aallinnot2 c ON a.mfdfsds = c.NDB_No
LEFT JOIN aalinfsds d ON a.fsdsnum = d.id1
WHERE d.own_id =42
GROUP BY id1 )
WHERE c.NDB_No
IN ( SELECT DISTINCT `fsdsnum`
FROM `aaingred`
WHERE `usernum` LIKE '42'
)
MySQL said:
#1093 - You can't specify target table 'c' for update in FROM clause
Unfortunately, I don't know how to get my values without referencing target table 'c'! Is there a workaround for this?
With the crazy table/column names and indecipherable logic, this might be the ugliest query I have ever seen. Congrats. :)
I think the following should work (or this approach). The main problem was untangling the group-by expression-- you need to give the database engine a dataset where each row in the target table is joined to a set that contains the updated value for that row. So here, select the new values in a sub-query, then join that sub-query to the original table.
EDIT Fixed some syntax
UPDATE
(
SELECT d.id1, SUM (c.Energ_Kcal) AS Sum_Energ_Kcal
FROM aaingred a
LEFT JOIN aaweight b ON a.unit = b.uni
LEFT JOIN aallinnot2 c ON a.mfdfsds = c.NDB_No
LEFT JOIN aalinfsds d ON a.fsdsnum = d.id1
WHERE d.own_id =42
GROUP BY id1
) d
,aaingred a, aallinnot2 d
SET Energ_Kcal = d.Sum_Energ_Kcal
WHERE d.id1 = a.fsdsnum
AND a.mfdfsds = aallinnot2.NDB_No
AND c.NDB_No IN (
SELECT DISTINCT `fsdsnum`
FROM `aaingred`
WHERE `usernum` LIKE '42'
);
I'm not sure about mysql, but with SQL Server the statement would be something like this:
UPDATE aallinnot2
SET Energ_Kcal = (
SELECT SUM( c.Energ_Kcal)
FROM aaingred a
LEFT JOIN aaweight b ON a.unit = b.uni
LEFT JOIN aallinnot2 c ON a.mfdfsds = c.NDB_No
LEFT JOIN aalinfsds d ON a.fsdsnum = d.id1
WHERE d.own_id =42)
WHERE c.NDB_No
IN ( SELECT DISTINCT `fsdsnum`
FROM `aaingred`
WHERE `usernum` LIKE '42')
You can't alias the table to be updated in the UPDATE clause, but you can in the FROM clause.
I have this query which works perfectly:
SELECT *
FROM Customer
WHERE SacCode IN
(
SELECT SacCode
FROM SacCode
WHERE ResellerCorporateID = 392
ORDER BY SacCode
)
AND CustomerID IN
(
SELECT CxID
FROM CustAppointments
WHERE AppRoomID IN
(
SELECT AppRoomID
FROM ClinicRooms
WHERE ClinID IN
(
SELECT ClinID
FROM AppClinics
WHERE ClinDate >='20090101'
AND ClinDate <='20091119'
)
)
)
However, I need to see the value of ClinDate (inside the last nested query)...
How do I do it?
Thanks.
I'd rewrite the query using joins. Then, you can access any data from any of the joined tables.
For example, you could rewrite your query like this:
SELECT c.*, ac.ClinDate
FROM Customer c
JOIN SacCode sc ON sc.SacCode = c.SacCode
JOIN CustAppointments ca ON ca.CustomerID = c.CustomerID
JOIN ClinicRooms cr ON cr.AppRoomID = ca.AppRoomID
JOIN AppClinic ac ON ac.ClinID = cr.ClinID
WHERE ac.ClinDate >='20090101'
AND ac.ClinDate <='20091119'
AND sc.ResellerCorporateID = 392
Think I'd use derived table in the FROM statement rather than 3 deep nested query, will allow you to access values and will look a LOT better.
You'll need to copy the subselects to the FROM clause or rewrite the query using JOINs.
it should look something like this:
SELECT c.*, a.ClinDate
FROM Customer c
inner join CustAppointments ca
inner join ClinicRooms cr
inner join AppClinics a
where c.SacCode IN
(
SELECT SacCode
FROM SacCode
WHERE ResellerCorporateID = 392
ORDER BY SacCode
)
and c.CustomerID = ca.CxID
and ca.AppRoomID = cr.AppRoomID
and cr.ClinID = a.ClinID
and a.ClinDate >='20090101'
and a.ClinDate <='20091119'