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
Related
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 having trouble figuring out this SQL Query (MySql 5 is the server).
My tables are related like this:
My Query looks like this:
select
tblCustomer.customerNo,
tblSalesHeader.invoiceNo,
tblSalesHeader.orderDate,
tblSalesDetail.quantity,
tblSalesDetail.productNo,
tblSalesDetail.description,
tblSalesDetail.unitPrice
FROM
tblSalesHeader
left JOIN tblSalesDetail ON (tblSalesHeader.invoiceNo = tblSalesDetail.invoiceNo)
left JOIN tblCustomer ON (tblSalesHeader.cID = tblCustomer.cid)
left JOIN tblUsers ON (tblSalesHeader.salesmanID = tblUsers.iID)
left JOIN tblProducts ON (tblSalesDetail.productNo = tblProducts.productNumber)
WHERE
tblSalesHeader.invoiceNo=2482
GROUP BY
tblCustomer.customerNo,
tblSalesHeader.invoiceNo,
tblSalesDetail.productNo
I know that there should be 5 rows returned because there are 5 rows in tblSalesDetail where the invoiceNo=2482 like so:
select *
from
tblSalesDetail
left join tblSalesHeader on (tblSalesHeader.invoiceNo = tblSalesDetail.invoiceNo)
where
tblSalesHeader.invoiceNo=2482
I'm sure that my joins are filtering results out but I don't know why.
Get rid of the GROUP BY. You're not using it.
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
Can someone tell me how do I write the following SQL:
SELECT url_source_wp.url
FROM url_source_wp
WHERE url_source_wp.id NOT IN (
SELECT url_done_wp.url_source_wp
FROM url_done_wp
WHERE (url_done_wp.url_group = 4) AND (hash IS NULL)) LIMIT 50;
using a join?
I tried:
SELECT url_source_wp.url
FROM url_source_wp
LEFT OUTER JOIN url_done_wp ON url_source_wp.id = url_done_wp.url_source_wp
WHERE url_done_wp.url_group = 4 AND url_source_wp.hash is NULL LIMIT 50
But the reply is not the same.
The problem is that the first SQL is very very slow.
I believe that you are looking for something like this:
SELECT url_source_wp.url
FROM url_source_wp
LEFT OUTER JOIN url_done_wp
ON url_source_wp.id = url_done_wp.url_source_wp AND url_done_wp.url_group = 4 AND hash IS NULL
WHERE url_done_wp.url_source_wp IS NULL
LIMIT 50
Shouldn't you just negate the two conditions in the WHERE clause ?
I assume you're trying to get all the url_source_wp records whose id's referenced in the url_done_wp table by the FK url_source_wp which do NOT have url_group = 4 and their hash column is NOT NULL, since you used a subquery with NOT IN.
INNER JOIN should be fine to.
So it should be:
SELECT url_source_wp.url
FROM url_source_wp
INNER JOIN url_done_wp ON url_source_wp.id = url_done_wp.url_source_wp
WHERE url_done_wp.url_group != 4 AND url_source_wp.hash IS NOT NULL LIMIT 50
I want to select all records from website.advert and join player name from different table also. The query runs itself without any errors, but there is a problem that it does select duplicated records from website.advert when there is more than one record with the same account_id in the player table.
Here is my query:
SELECT `advert`.*, `p`.`name`, `p`.`level`
FROM `website`.`advert`
LEFT JOIN `player`.`player` `p`
ON (`p`.`account_id` = `website`.`advert`.`author_id`)
WHERE `advert`.`accepted` = 0
You need to use DISTINCT but I am having trouble figuring out your query. You don't seem to be selecting any fields from website.advert
Sorry, edited:
SELECT `advert`.*, `p`.`name`, `p`.`level`
FROM `website`.`advert`
LEFT JOIN `player`.`player` `p`
ON (`p`.`account_id` = `website`.`advert`.`author_id`)
WHERE `advert`.`accepted` = 0
GROUP BY `advert`.`id`
We are JUST selecting the id field for now - try that and see if it gives you closer to the results you are expecting.
Try this query:
SELECT a.*, p.name, p.level
FROM advert a LEFT JOIN player p
ON (p.account_id = a.author_id)
WHERE a.accepted = 0
group by p.account_id