Mysql query join table and let fill with empty if value same - mysql

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

Related

Multiple Value Select with Inner Join 3 table

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.

mysql select all rows in left and let null in right table

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

Mysql inner join query showing wrong result

I have two tables in MySQL
and
I want to show Categories in place of Cid in first table. I'm using following query:
SELECT id,categorymass.Cid,video.Name
from video inner join categorymass on video.Cid = categorymass.Cid
What is problem in my inner join
Well You are not selecting category...
SELECT id,categorymass.Cid, categorymass.Category,video.Name
from video inner join categorymass on video.Cid = categorymass.Cid
Try this it will work :
SELECT t2.`Cid`, t2.`Category`,t1.`Name`
from video t1 JOIN categorymass t2 on t1.`Cid` = t2.`Cid`

MS Access Multi-Join Query

Can anyone find what is wrong with this MS Access Query? When I try to execute it i receive an error about a missing Operator before the 2nd Left Join
SELECT * FROM (
SELECT GetitUsageTemp.MemberID,
GetitUsageTemp.IDNumber,
GetitUsageTemp.Title,
GetitUsageTemp.Initials,
GetitUsageTemp.Forenames,
GetitUsageTemp.Surnames,
GetitUsageTemp.CellNumber,
GetitUsageTemp.EmailAddress,
Nz(August.[AugustUsage],0) AS AugustUsage
FROM GetitUsageTemp
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS JulyUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #07/01/2013# And #08/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) Requests
ON GetitUsageTemp.MemberID = Requests.fk_Members_ID
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS AugustUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #08/01/2013# And #09/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) August
ON GetitUsageTemp.MemberID = August.fk_Members_ID
)GETIT
In Access you can only join two tables. If you need to join more tables, you need to group the first join together using parentheses, as if it was a new derived table. Then you can join another table to that group:
select
*
from
( ( Table1
LEFT JOIN Table2 ...
)
LEFT JOIN Table3 ...
)
LEFT JOIN Table4 ...
(I'm using awkward indentation to try to make the groups more clear)

Left join using a condition

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