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.
Related
I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.
I Have problem with joining 2 table
this is mysql query
select *
from tbl_perspective a
inner join tbl_objective b on b.idperspective=a.idperspective
The result is:
Query Result
I Want to display first row of perspectivename and blank or null
Final Result:
enter image description here
Hi Anwr Rawk simply you can use LEFT JOIN
select * from tbl_perspective as a
left join
tbl_objective as b
on b.idperspective=a.idperspective
Join to a subquery which identifies the first row for each idperspective group:
SELECT t1.*
FROM tbl_perspective t1
INNER JOIN
(
SELECT idperspective, MIN(idobjective) AS min_idobjective
FROM tbl_perspective
GROUP BY idperspective
) t2
ON t1.idperspective = t2.idperspective AND
t1.idobjective = t2.min_idobjective;
You need to use LEFT JOIN to include all results, like this : https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
I have two tables in MySql Database, the tables is:
upper one table is jenis_pohon and the lower one is jtt
how to select all rows in jenis_pohon and join it with jtt with condition jtt.tahun='2016' AND jtt.koperasi_id='4'
I tried the following query:
SELECT * FROM `jenis_pohon` LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id=jtt.jenis_pohon_id WHERE jtt.tahun='2016' AND jtt.koperasi_id='4'
but no luck.
basicaly I want to have 6 rows returned (this 6 rows is come from jenis_pohon).
I've Created Your tables and tested it, it works.
Use This query :
SELECT * FROM jenis_pohon
LEFT JOIN jtt ON (jenis_pohon.jenis_pohon_id = jtt.jenis_pohon_id AND jtt.tahun = '2016' AND jtt.koperasi_id = '4')
Notice that using AND inside join parenthesis make your query mush faster than using WHERE.
The result is :
You can sort it like :
SELECT * FROM jenis_pohon
LEFT JOIN jtt ON (jenis_pohon.jenis_pohon_id = jtt.jenis_pohon_id AND jtt.tahun = '2016' AND jtt.koperasi_id = '4')
ORDER BY jenis_pohon.jenis_pohon_id
Check this. Check here Demo
Table jtt with condition jtt.tahun='2016' AND jtt.koperasi_id='4'
it returns only 3 records because jtt.koperasi_id='4' ave only 3 records into tabel 'jtt'
SELECT * FROM `jenis_pohon`
LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id=jtt.jenis_pohon_id
WHERE jtt.tahun='2016' AND jtt.koperasi_id='4';
O Table jtt with condition onlt jtt.tahun='2016'
SELECT distinct * FROM `jenis_pohon`
LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id=jtt.jenis_pohon_id
WHERE jtt.tahun='2016'
Try using below Query.
SELECT
*
FROM
`jenis_pohon`
LEFT JOIN jtt ON jenis_pohon.jenis_pohon_id = jtt.jenis_pohon_id
AND jtt.tahun = '2016'
AND jtt.koperasi_id = '4';
LIVE DEMO
OUTPUT
I have 3 tables "job_messages" contain 3 records and these all I want along with names from other 2 tables that are linked using foreign keys
here are the 3 tables:
Here are the SQL
SELECT
`job_messages`.`job_message_id`,
`job_messages`.`job_time`,
`job_messages`.`job_type`,
`job_messages`.`work_group_id`,
`job_messages`.`location_id`,
`job_messages`.`patient_name`,
`job_messages`.`room_no`,
`job_messages`.`test_taken`,
`job_messages`.`worker_id`,
`job_messages`.`notes`,
`work_group`.`work_group`,
`locations`.`location_name`
FROM
`job_messages`
INNER JOIN `work_group` ON (`job_messages`.`work_group_id` = `work_group`.`work_group_id`)
INNER JOIN `locations` ON (`work_group`.`work_group_id` = `locations`.`work_group_id`)
The problem I get 12 rows from this select along with repeated records and all I want are the main 3 records from "job_messages", what the correct SQL should be ?
Have you tryed changing Joining locations?
INNER JOIN `locations` USING (`location_id`)
Or, if you prefer to use ON:
INNER JOIN `locations` ON (`job_messages`.`location_id` = `locations`.`location_id``)
You say your table job_messages contain 3 rows, but you dont say how many rows are in the other two tables.
Your query looks ok, so i will guess the problem is the data.
try this querys.
SELECT `work_group_id`, count(*)
FROM `work_group`
GROUP BY `work_group_id`
HAVING count(*) > 1
SELECT `work_group_id`, count(*)
FROM `locations`
GROUP BY `work_group_id`
HAVING count(*) > 1
if any of those query return result you have a problem and that cause the duplicated rows.
EDIT
If you have multiple locations try using this
FROM `job_messages`
INNER JOIN `work_group`
ON (`job_messages`.`work_group_id` = `work_group`.`work_group_id`)
INNER JOIN `locations`
ON (`job_messages`.`location_id` = `locations`.`location_id`)
OR
FROM `job_messages`
INNER JOIN `work_group`
ON (`job_messages`.`work_group_id` = `work_group`.`work_group_id`)
INNER JOIN `locations`
ON (`work_group`.`work_group_id` = `locations`.`work_group_id`)
AND (`job_messages`.`location_id` = `locations`.`location_id`)
Currently I am using the following query to display the following result.
SELECT * FROM RouteToGrowthRecord, GradeMaster,MileStoneMaster
WHERE MemberID = 'ALV01L11034A06' AND
RouteToGrowthRecord.GradeID=GradeMaster.GradeID AND
RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
ORDER BY CheckupDate DESC
Now I have another table named RouteToGrowthRecord_st that has same
columns as RouteToGrowthRecord with some additional fields.
I need to display result that are present in both the table. ie . if RouteToGrowthRecord_st has 3 records with the given menberID,then output must contain 3 more records along with the above query result.(fr ex above its 9+3=12 records in total).
You can use Union here to merge the results getting from both queries. Use default values for the unmapped additional fields.
You can write above query in following way
SELECT * FROM RouteToGrowthRecord
INNER JOIN GradeMaster ON RouteToGrowthRecord.GradeID=GradeMaster.GradeID
INNER JOIN MileStoneMaster ON RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
LEFT JOIN RouteToGrowthRecord_st ON RouteToGrowthRecord_st.memberID=RouteToGrowthRecord.memberID
WHERE
RouteToGrowthRecord.MemberID = 'ALV01L11034A06'
order by CheckupDate DESC
this is my answer
SELECT CheckUpDate,AgeInMonths,PresentWeight,Height,Diagnosis,growthstatus,GradeName,MilestoneName,MemberID
FROM RouteToGrowthRecord, GradeMaster,MileStoneMaster WHERE
MemberID = 'ALV01L56107A11 ' and
RouteToGrowthRecord.GradeID=GradeMaster.GradeID and
RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
union
SELECT CheckUpDate,AgeInMonths,PresentWeight,Height,Diagnosis,growthstatus,GradeName,MilestoneName,MemberID
FROM RouteToGrowthRecord_st, GradeMaster,MileStoneMaster WHERE
MemberID = 'ALV01L56107A11 ' and
RouteToGrowthRecord_st.GradeID=GradeMaster.GradeID and
RouteToGrowthRecord_st.MileStoneID=MileStoneMaster.MileStoneID
order by CheckupDate DESC
SELECT * FROM RouteToGrowthRecord a inner join GradeMaster b inner
join MileStoneMaster c inner join RouteToGrowthRecord_st d on
a.GradeID=b.GradeID AND a.MileStoneID=c.MileStoneID and
d.GradeID=b.GradeID AND d.MileStoneID=c.MileStoneID
WHERE a.MemberID = 'ALV01L11034A06'
ORDER BY CheckupDate DESC