How can I combine Join these two tables? - mysql

How can I combine Join these two tables?
Table 1
SELECT job_category.JobCategoryId, job_category.JobCategoryName, count(job_position.JobCategoryId)
AS AvailableCategories
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName
Table 2
(SELECT job_category.JobCategoryId, job_category.JobCategoryName, count(job_position.ContactId) AS AllocatedJobs
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
WHERE job_position.ContactId > 0
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName)
Thanks

I think it should be like this
Select T1.JobCategoryId, T1.JobCategoryName, T1.AvailableCategories, T2.AllocatedJobs
FROM
(SELECT job_category.JobCategoryId as JobCategoryId, job_category.JobCategoryName as JobCategoryName, count(job_position.JobCategoryId)
AS AvailableCategories
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName) as T1
INNER JOIN
((SELECT job_category.JobCategoryId as T2JobCategoryId, job_category.JobCategoryName as T2JobCategoryName, count(job_position.ContactId) AS AllocatedJobs
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
WHERE job_position.ContactId > 0
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName)) as T2
ON T1.JobCategoryId = T2.T2JobCategoryId

Related

How not to include the ID in the result of the query when using Left Join

Im trying to populate my listview with data from three tables stu_tblbasicinfo,stu_tblfam and stu_tbleducbackground
they all have the same id which is TagID
Select A.TagID , A.Surname, A.Firstname, A.Middlename, A.Course, A.Year, B.ZipCode,
B.Province, B.Municipality, B.Barangay, A.ContactNo, A.EmailAddress, A.Birthdate,
A.Age, A.Birthplace, A.Religion, A.Gender, A.CivilStatus, A.Spouse, C.Mothersname,
C.M_occupation, C.M_Number, C.Fathersname, C.F_Occupation, C.F_Number,
C.GuardianName, C.G_Occupation, C.G_Number, D.Elementary, D.E_Years, D.JuniorHigh,
D.J_Years, Seniorhigh, D.S_Years
from stu_tblbasicinfo As A
left join stu_tblzipcode As B
on A.Barangay = B.Barangay
inner join stu_tblfam As C
ON A.TagID = C.TagID
inner join stu_tbleducbackground As D
ON A.TagID = D.TagID
My code works but the problem is it displays 3 TagID.
The result is like this
TagID(studentinfo table),Surname,Firstname,Middlename,TagID(familyBackground table),MName,FName,Gname,TagID(EducBackground table),Elementary,E_years,JuniorHigh,J_Years,SeniorHigh,S_Years
How can I make the result in this format?
TagID,Surname,Firstname,Middlename,Mname,Fname,Gname,Elementary,E.Years,JuniorHigh,J_Years,SeniorHigh,S_Years
Just this:
Select
A.TagID ,
A.Surname,
A.Firstname,
A.Middlename,
C.Mothersname,
C.Fathersname,
C.GuardianName,
D.Elementary,
D.E_Years,
D.JuniorHigh,
D.J_Years,
D.Seniorhigh,
D.S_Years
from stu_tblbasicinfo As A
left join stu_tblzipcode As B
on A.Barangay = B.Barangay
inner join stu_tblfam As C
ON A.TagID = C.TagID
inner join stu_tbleducbackground As D
ON A.TagID = D.TagID

MySQL INNER JOIN returns zero results if all values are not found

I have the following query that works great when all of the INNER JOIN criteria is met. Unfortunately, this query returns zero results when for example INNER JOIN TableSample6 ON TableSample1.Field4 = TableSample6.Field6 is not found.
Can I restructure this query to return results even if some of the INNER JOIN values do not have a match in some cases?
SELECT DISTINCT
TableSample1.Field4,
TableSample.Field2,
TableSample2.Field2,
TableSample3.Field3,
TableSample4.Field4,
TableSample5.Field1,
TableSample.Field3,
TableSample6.Field1,
TableSample6.Field2,
TableSample7.Field7,
TableSample3.Field4,
TableSample4.Field2,
TableSample5.Field4
FROM TableSample1
INNER JOIN TableSample2 ON TableSample1.Field2 = TableSample2.Field4
INNER JOIN TableSample ON TableSample1.Field6 = TableSample.Field4
INNER JOIN TableSample3 ON TableSample1.Field3 = TableSample3.Field2
INNER JOIN TableSample4 ON TableSample1.Field7 = TableSample4.Field3
INNER JOIN TableSample5 ON TableSample1.Field8 = TableSample5.Field2
INNER JOIN TableSample6 ON TableSample1.Field4 = TableSample6.Field6
WHERE (((TableSample1.Field4)="xxxxxx" AND (TableSample.Field2)="xxxxxx"))
ORDER BY TableSample1.Field2;
You could use LEFT JOIN:
SELECT DISTINCT
t1.Field4,
t.Field2,
t2.Field2,
t3.Field3,
t4.Field4,
t5.Field1,
t.Field3,
t6.Field1,
t6.Field2,
t7.Field7, -- PROBLEM: you never join to TableSample7
t3.Field4,
t4.Field2,
t5.Field4
FROM TableSample1 t1
LEFT JOIN TableSample2 t2
ON t1.Field2 = t2.Field4
LEFT JOIN TableSample t
ON t1.Field6 = t.Field4
LEFT JOIN TableSample3 t3
ON t1.Field3 = t3.Field2
LEFT JOIN TableSample4 t4
ON t1.Field7 = t4.Field3
LEFT JOIN TableSample5 t5
ON t1.Field8 = t5.Field2
LEFT JOIN TableSample6 t6
ON t1.Field4 = t6.Field6
WHERE t1.Field4 = "xxxxxx" AND
t.Field2 = "xxxxxx"
ORDER BY t1.Field2;
You could also use COALESCE() to handle records which do not match to all of the tables. For example, if TableSample2.Field2 were varchar, you could use COALESCE(t2.Field2, 'NA') to show NA instead of NULL.

MS-Access Joining 2 Subqueries

Need a little help with subqueries
If I have a 1 query like this:
SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
...and another like this:
SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
...and I wanted to do a join between the 2 queries (LEFT JOIN on the common Learner_Id and LatestEmpDate / DateEmpStatApp fields), how would I go about doing all this work in a single query where the 2 queries above would be subqueries?
My attempt below is not being accepted (JOIN expression not supported):
SELECT sQ1.Learner_Id, sQ1.LearnRefNumber, sQ1.FamilyName, sQ1.GivenNames, sQ1.LatestEmpDate, sQ1.ESMType, sQ2.ESMCode
FROM
(SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType) As sQ1
LEFT JOIN
(SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id) As sQ2
ON (sQ1.Learner_Id = sQ2.Learner_Id) AND (sQ1.LatestEmpDate = sQ2.DateEmpStatApp);
Would something like this get you what you want...?
SELECT l.Learner_Id, d.LatestEmpDate, l.LearnRefNumber, l.FamilyName, l.GivenNames, m.ESMType, m.ESMCode
FROM ((Learner AS l
LEFT JOIN (
SELECT s.Learner_Id, MAX(s.DateEmpStatApp) AS LatestEmpDate
FROM LearnerEmploymentStatus AS s
GROUP BY s.Learner_Id) AS d ON d.Learner_Id = l.Learner_Id)
LEFT JOIN LearnerEmploymentStatus AS ls ON (ls.Learner_Id = d.Learner_Id) AND (ls.DateEmpStatApp = d.LatestEmpDate))
LEFT JOIN EmploymentStatusMonitoring AS m ON m.LearnerEmploymentStatus_Id = ls.LearnerEmploymentStatus_Id
WHERE m.ESMType = 'BSI'
Assumes the same learner won't have the same DateEmpStatApp twice, which may or not be valid.

How to use a case statement to determine which table to left join

How to use a case statement to determine which table to left join to this sql sentence:
SELECT c.dentist_id,
c.pack_id,
c.check_id,
sql1.vendor_no ,
p.pack_trans_id,
r.lot_no,
IF(ml.payment_type='Prepay','Pre-Paying','Post-Paying') payment_type,
ml.image_path,
ml.metal_id,
tp.total_paid_refiner,
sql1.percentage,
sql1.contact_percentage,
sql1.agent_percentage,
c.agent_dentist,
c.check_amt,
c.check_image,
c.check_date_sent,
c.check_no,
c.check_clear_date,
ml.spot_gold_price,
ml.total_weight,
ml.estimated_price,
ml.expect_more,
ml.how_long_acquire,
sa.full_name,
pd.company_name,
pd.phone_no,
pd.email
FROM tbl_check AS c
LEFT JOIN tbl_pack_list AS p ON p.pack_id=c.pack_id
LEFT JOIN tbl_pack_list_details AS pd ON pd.pack_id=c.pack_id
LEFT JOIN
(SELECT vendor_no,
paying_contact,
contact_percentage,
percentage,
pay_to,
agent_percentage,
dentist_id did2,
sales_agent_id
FROM tbl_mst_dentist
GROUP BY dentist_id) sql1 ON sql1.did2=c.dentist_id
LEFT JOIN tbl_refining r ON r.pack_id=c.pack_id
LEFT JOIN tbl_metals_list ml ON ml.pack_id=c.pack_id
LEFT JOIN tbl_payment tp ON tp.pack_id=c.pack_id
LEFT JOIN tbl_sales_agent sa ON sa.sales_agent_id=sql1.sales_agent_id
LEFT JOIN
(SELECT * CASE c.agent_dentist WHEN 'a' THEN
(SELECT sa.* ,
sm.state_code
FROM tbl_sales_agent AS sa,
tbl_mst_state AS sm
WHERE sa.sales_agent_id = c.sale_agent_id
AND sa.state=sm.state_name) ELSE
(SELECT *
FROM tbl_mst_dentist
WHERE dentist_id = c.dentist_id))
WHERE 1
AND c.paying_percent !=0
ORDER BY p.pack_trans_id DESC

Joining results of two independent query

I have two working queries:
1st returns the product list:
$sql = "
SELECT a.stockID, a.stockCatID, a.stockName, a.stockCode, a.stockCatCode,
CONCAT_WS(' » ', d.stockCatName, c.stockCatName, b.stockCatName) AS stockPath,
a.stockName AS stockTitle, a.stockID AS uniStock
FROM stockcards a
LEFT OUTER JOIN stockcategories b
ON a.stockCatID = b.stockCatID
LEFT OUTER JOIN stockcategories c
ON b.stockParentCat = c.stockCatID
LEFT OUTER JOIN stockcategories d
ON c.stockParentCat = d.stockCatID ";
2nd is basically returns the difference of total received and total sent whic is remaining quantity:
SELECT DISTINCT (COALESCE(o.totalReceived, 0) + COALESCE(p.totalSent, 0)) as RemainingStock
FROM deliverydetails k
INNER JOIN stockcards l ON k.stockID= l.stockID
LEFT JOIN
(
SELECT m.stockID, SUM(m.dQuantity) totalReceived
FROM deliverydetails m
WHERE m.dQuantity > 0
GROUP BY m.stockID
)
o ON k.stockID = o.stockID
LEFT JOIN
(
SELECT n.stockID, SUM(n.dQuantity) totalSent
FROM deliverydetails n
WHERE n.dQuantity < 0
GROUP BY n.stockID
)
p ON k.stockID = p.stockID
I need to add a new column to first query to display remanining quantity. But couldn't succeed to join this two. Thanks for any tip.
As it seems that your 2nd query just sums positive and negative dQuantities for each stockID, and then adds them together, I think this small change to your 1st query is
all that is needed:
SELECT a.stockID, a.stockCatID, a.stockName, a.stockCode, a.stockCatCode,
CONCAT_WS(' » ', d.stockCatName, c.stockCatName, b.stockCatName) AS stockPath,
a.stockName AS stockTitle, a.stockID AS uniStock,
dd.RemainingStock AS RemainingStock
FROM stockcards a
LEFT OUTER JOIN stockcategories b
ON a.stockCatID = b.stockCatID
LEFT OUTER JOIN stockcategories c
ON b.stockParentCat = c.stockCatID
LEFT OUTER JOIN stockcategories d
ON c.stockParentCat = d.stockCatID
LEFT OUTER JOIN
(SELECT stockID, SUM(dQuantity) AS RemainingStock
FROM deliverydetails
GROUP BY stockID) AS dd
ON dd.stockID = a.stockID