My database tables, i need to get the "NEW_IC_NO" from these two tables if they found matched.
TB_A
TB_B
Don't use CASE WHEN, use a JOIN:
SELECT a.NEW_IC_NO
FROM TB_A a
INNER JOIN TB_B b ON a.NEW_IC_NO = b.NEW_IC_NO
Use inner join or any join..
Example
Select * from TB_A as A inner join TB_B as B on A.NEW_IC_NO = B.NEW_IC_NO.
Related
how to Get Value not multiple per Result
SELECT annual_leave.id_annual_leave, approve_cuti_tanggal.tanggal_request_izin, tbl_timesheet.shift FROM annual_leave
INNER JOIN approve_cuti_tanggal ON approve_cuti_tanggal.id_annual = annual_leave.id_annual_leave
INNER JOIN tbl_timesheet ON tbl_timesheet.id_annual = annual_leave.id_annual_leave
WHERE approve_cuti_tanggal.keterangan='APPROVAL HEAD NOC' AND annual_leave.status='APPROVAL'
From Table Approval_tanggal Only 2 Value
And tbl_timesheet too
I think you can use
select distinct * from <table_name>
You can refer to MySQL documentation.
this is how i manage to join the table using inner join
SELECT lab5enrollment.matricno, lab5student.stuname,
lab5enrollment.courseid,
lab5course.cname
FROM ((lab5enrollment
INNER JOIN lab5student ON lab5enrollment.matricno = lab5student.matricno)
INNER JOIN lab5course ON lab5enrollment.courseid = lab5course.courseid)
WHERE lab5enrollment.courseid = 'CSF3402';
this is how i used the using keyword to join the table but i dont know how to join the three table...
SELECT matricno, stuname, courseid, cname
FROM lab5enrollment
JOIN lab5student
USING (matricno)
WHERE courseid = 'CSF3402';
i want to observe the differrence between using the inner join and using...
You should probably lean towards using joins with explicit ON clauses for a number of reasons. If you wanted to use USING here, then the following should work:
SELECT
t1.matricno,
t2.stuname,
t1.courseid,
t3.cname
FROM lab5enrollment t1
INNER JOIN lab5student t2
USING (matricno)
INNER JOIN lab5course t3
USING (courseid)
WHERE
t1.courseid = 'CSF3402';
This assumes that lab5enrollment and lab5student both have a column with the same name matricno, and that lab5student and lab5course both have a column called courseid.
I have this issue.
I need to get this fields from my matching table:
date, points
Then i have an epos_id field as well in my matching table..
I have another rbpos_epos table which has epos_id and location field.
I need to get the location from the rbpos_epos using joins.. something like this:
SELECT matching.date, matching.points, matching.time,matching.location,matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
FROM matching WHERE matching.user_id="'.$id_user.'"
LEFT JOIN rbpos_epos where matching.epos_id=rbpos_epos.epos_id;
SELECT
first_table.date,
first_table.points,
first_table.time,
first_table.location,
first_table.epos_id,
rbpos_epos.epos_id,
rbpos_epos.location
FROM
first_table
LEFT JOIN
rbpos_epos ON first_table.epos_id = rbpos_epos.epos_id
WHERE
first_table.user_id = "'.$id_user.'";
Use on instead where -
SELECT matching.date, matching.points, matching.time,matching.location,matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
FROM matching LEFT JOIN rbpos_epos ON matching.epos_id=rbpos_epos.epos_id
WHERE matching.user_id="'.$id_user.'";
Try this:
SELECT m.date, m.points, m.time,m.location,m.epos_id,re.epos_id,re.location
FROM matching m
LEFT JOIN rbpos_epos re ON m.epos_id=re.epos_id
WHERE m.user_id= 10
I have this cross-database query...
SELECT
`DM_Server`.`Jobs`.*,
`DM_Server`.servers.Description AS server,
digital_inventory.params,
products.products_id,
products.products_pdfupload,
customers.customers_firstname,
customers.customers_lastname
FROM `DM_Server`.`Jobs`
INNER JOIN `DM_Server`.servers ON servers.ServerID = Jobs.Jobs_ServerID
JOIN `cpod_live`.`digital_inventory` ON digital_inventory.jobname = Jobs.Jobs_Name
JOIN `cpod_live`.`products` ON products.products_pdfupload = CONCAT(digital_inventory.jobname, ".pdf")
JOIN `cpod_live`.`customers` ON customers.customers_id = products.cID
ORDER BY `DM_Server`.`Jobs`.Jobs_StartTime DESC LIMIT 50
it runs fine until I make them LEFT JOINs. I thought that by not specifying a type of join it was assumed to be a LEFT JOIN. Is this not the case?
I thought that by not specifying a type of join it was assumed to be a LEFT JOIN. Is this not the case?
No, the default join is an INNER JOIN.
Here is a visual explanation of SQL joins.
Inner join
Left join
No. When a type isn't specified, an INNER JOIN is used. To read up on differences; wikipedia
I believe the default is INNER JOIN if you just specify JOIN.
If you just mentioned JOIN in query by default it will be considered
as a INNER JOIN.
Left join:Left join will take all the elements from Left table and only matching records from the Right table as Follows.
example:
SELECT column_name(s)
FROM table_name1 #(Left table)
LEFT JOIN table_name2 #(Right table)
ON table_name1.column_name=table_name2.column_name
Hope this helps.
I want to replace the subquery with a join, if possible.
SELECT `fftenant_farmer`.`person_ptr_id`, `fftenant_surveyanswer`.`text_value`
FROM `fftenant_farmer`
INNER JOIN `fftenant_person`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_person`.`id`)
LEFT OUTER JOIN `fftenant_surveyanswer`
ON fftenant_surveyanswer.surveyquestion_id = 1
AND fftenant_surveyanswer.`surveyresult_id` IN (SELECT y.`surveyresult_id` FROM `fftenant_farmer_surveyresults` y WHERE y.farmer_id = `fftenant_farmer`.`person_ptr_id`)
I tried:
SELECT `fftenant_farmer`.`person_ptr_id`, `fftenant_surveyanswer`.`text_value`#, T5.`text_value`
FROM `fftenant_farmer`
INNER JOIN `fftenant_person`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_person`.`id`)
LEFT OUTER JOIN `fftenant_farmer_surveyresults`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_farmer_surveyresults`.`farmer_id`)
LEFT OUTER JOIN `fftenant_surveyanswer`
ON (`fftenant_farmer_surveyresults`.`surveyresult_id` = `fftenant_surveyanswer`.`surveyresult_id`)
AND fftenant_surveyanswer.surveyquestion_id = 1
But that gave me one record per farmer per survey result for that farmer. I only want one record per farmer as returned by the first query.
A join may be faster on most RDBMs, but the real reason I asked this question is I just can't seem to formulate a join to replace the subquery and I want to know if it's even possible.
You could use DISTINCT or GROUP BY, as mvds and Brilliand suggest, but I think it's closer to the query's design intent if you change the last join to an inner-join, but elevating its precedence:
SELECT farmer.person_ptr_id, surveyanswer.text_value
FROM fftenant_farmer AS farmer
INNER
JOIN fftenant_person AS person
ON person.id = farmer.person_ptr_id
LEFT
OUTER
JOIN
( fftenant_farmer_surveyresults AS farmer_surveyresults
INNER
JOIN fftenant_surveyanswer AS surveyanswer
ON surveyanswer.surveyresult_id = farmer_surveyresults.surveyresult_id
AND surveyanswer.surveyquestion_id = 1
)
ON farmer_surveyresults.farmer_id = farmer.person_ptr_id
Broadly speaking, this will end up giving the same results as the DISTINCT or GROUP BY approach, but in a more principled, less ad hoc way, IMHO.
Use SELECT DISTINCT or GROUP BY to remove the duplicate entries.
Changing your attempt as little as possible:
SELECT DISTINCT `fftenant_farmer`.`person_ptr_id`, `fftenant_surveyanswer`.`text_value`#, T5.`text_value`
FROM `fftenant_farmer`
INNER JOIN `fftenant_person`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_person`.`id`)
LEFT OUTER JOIN `fftenant_farmer_surveyresults`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_farmer_surveyresults`.`farmer_id`)
LEFT OUTER JOIN `fftenant_surveyanswer`
ON (`fftenant_farmer_surveyresults`.`surveyresult_id` = `fftenant_surveyanswer`.`surveyresult_id`)
AND fftenant_surveyanswer.surveyquestion_id = 1
the real reason I asked this question is I just can't seem to formulate a join to replace the subquery and I want to know if it's even possible
Then consider a much simpler example to begin with e.g.
SELECT *
FROM T1
WHERE id IN (SELECT id FROM T2);
This is known as a semi join and if desired may be re-written using (among other possibilities) a JOIN with a SELECT clause to a) project only from the 'outer' table, and b) return only DISTINCT rows:
SELECT DISTINCT T1.*
FROM T1
JOIN T2 USING (id);