Using this query
SELECT DISTINCT(job_primary.id) AS id
FROM `job_primary`
LEFT JOIN `job_skill` ON `job_skill`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_facts` ON `job_facts`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_location` ON `job_location`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_people` ON `job_people`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_sec` ON `job_sec`.`job_id` = `job_primary`.`id`
AND job_sec.exp_start_date > UNIX_TIMESTAMP()
INNER JOIN `user_skills` ON `user_skills`.`skill_id` = `job_skill`.`skill_id`
AND user_skills.user_id = 1
WHERE job_primary.posted_by != 1
I am getting only the id value. But I want to get the rest of the values with distinct id. Don't think about rest of the codes. The problem is here, if I use:
select *, job_primary.id as id
to select code then I get all data. But it is not distinct. So if I used
SELECT *, distinct(job_primary.id) as id
or
SELECT distinct(job_primary.id), * as id
but it is showing errors.
I think your confussion is what to query itself, nixon1333. What do you want to ask to the database? If you want just one row per id, group'em by with SELECT * FROM ... GROUP BY job_primary.id
Related
I have a table:
and this table:
I would like to create report like this
I've tried this SQL:
select
master_problem.problem,
master_problem.sop_reference,
master_problem.adidas_spec,
count(log_roving_qc.id_problem) as jumlahfrom
master_problem
inner join log_roving_qc
on master_problem.id_problem = log_roving_qc.id_problem
group by master_problem.id_problem
but the empty data does not show. I want to display blank data with a description of 0
Do a left join of the master_problem table to a subquery which does the count aggregation:
SELECT
mp.problem,
mp.sop_reference,
mp.adidas_spec,
COALESCE(t.cnt, 0) AS jumlahfrom
FROM master_problem mp
LEFT JOIN
(
SELECT id_problem, COUNT(*) as cnt
FROM log_roving_qc
GROUP BY id_problem
) t
ON mp.id_problem = t.id_problem;
I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan
Is there a way that I can combine these two queries:
FIRST QUERY
select top 100
WORK.pzInsKey,
WORK.pyID,
PARTY.MacID,
PARTY.OtherPartyID,
PARTY.CustomerEmail,
ACCOUNT.AccountNumber,
ACCOUNT.AccountName,
ACCOUNT.AdviserCode,
ACCOUNT.AdviserName,
ACCOUNT.DealerCode,
ACCOUNT.DealerName,
ACCOUNT.PrimaryAccount,
ACCOUNT.ProductCategory,
ACCOUNT.ProductCode,
ACCOUNT.ProductDescription,
ACCOUNT.RegisteredState,
DOCUMENT.UDOCID
from
workTable WORK,
partyTable PARTY,
accountTable ACCOUNT,
documentTable DOCUMENT,
notesTable NOTES
where WORK.pzInsKey = PARTY.pxInsIndexedKey
and WORK.pzInsKey = ACCOUNT.pxInsIndexedKey
and WORK.pyID = DOCUMENT.CaseID
and SECOND QUERY
SELECT top 100
BusinessAreaTbl.businessarea,
ProcessTbl.process,
SubProcessTbl.subprocess
FROM workTable WORK
LEFT OUTER JOIN (SELECT DISTINCT Product_ID businessarea_id, Product businessarea from CaseTypesTable) BusinessAreaTbl
ON WORK.RequestBusinessArea#1 = BusinessAreaTbl.businessarea_id
LEFT OUTER JOIN (SELECT DISTINCT Process_ID, Process, Product_ID businessarea_id from CaseTypesTable) ProcessTbl
ON WORK.RequestProcess#1 = ProcessTbl.process_id
AND ProcessTbl.businessarea_id = WORK.RequestBusinessArea#1
LEFT OUTER JOIN (SELECT DISTINCT SubProcess_ID, SubProcess, Product_ID businessarea_id, Process_ID from CaseTypesTable) SubProcessTbl
ON WORK.RequestSubProcess#1 = SubProcessTbl.subprocess_id
AND SubProcessTbl.businessarea_id = WORK.RequestBusinessArea#1
AND SubProcessTbl.process_id = WORK.RequestProcess#1
It's basically two queries which produce separate results, but each query includes data from the workTable. In the 2nd query, the workTable data is derived from the CaseTypesTable.
I essentially just want the businessarea, process, and subprocess fields to be included with the results of the first query.
Thanks in advance for any help.
This should work:
(SELECT top 100
w.pzInsKey,
w.pyID,
p.MacID,
p.OtherPartyID,
p.CustomerEmail,
a.AccountNumber,
a.AccountName,
a.AdviserCode,
a.AdviserName,
a.DealerCode,
a.DealerName,
a.PrimaryAccount,
a.ProductCategory,
a.ProductCode,
a.ProductDescription,
a.RegisteredState,
d.UDOCID
FROM workTable w
LEFT JOIN partyTable p
ON w.pzInsKey = p.pxInsIndexedKey
LEFT JOIN accountTable a
ON w.pzInsKey = a.pxInsIndexedKey
LEFT JOIN documentTable d
ON w.pyID = d.CaseID)
UNION
(SELECT top 100
ba.businessarea,
pr.process,
spr.subprocess
FROM workTable w
LEFT OUTER JOIN (SELECT DISTINCT Product_ID businessarea_id, Product businessarea from CaseTypesTable) BusinessAreaTbl ba
ON w.RequestBusinessArea#1 = ba.businessarea_id
LEFT OUTER JOIN (SELECT DISTINCT Process_ID, Process, Product_ID businessarea_id from CaseTypesTable) ProcessTbl pr
ON w.RequestProcess#1 = pr.process_id
AND pr.businessarea_id = w.RequestBusinessArea#1
LEFT OUTER JOIN (SELECT DISTINCT SubProcess_ID, SubProcess, Product_ID businessarea_id, Process_ID from CaseTypesTable) SubProcessTbl spr
ON w.RequestSubProcess#1 = spr.subprocess_id
AND spr.businessarea_id = w.RequestBusinessArea#1
AND spr.process_id = w.RequestProcess#1))
Use the keyword UNION to combine two or more seperate SELECT statements.
I have the following mySql select statement it returns the below result and battling to get the result I am after.
select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
if(count(distinct `episode`.`c12`),
count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`
from
((((`tvshow`
left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
group by `tvshow`.`idShow`
having (count(`episode`.`c12`) > 0)
Select Result
I am trying to get a 4th column that would have the seasons listed in it e.g "Season 1,Season 2,Season 3"
I can get the the data I need by running the following select
select distinct c12 from episode where idShow = 1
It returns the following.
So i thought I could use the replace to change the results to read "Season1" but not sure how to get it to just return one string containing "Seasin1,Season2,Season3" and then add it to the select statement at the top of the view and bring it all together?
The Result I am trying to get(used Photoshop for this just so you could get the idea)
Just add GROUP_CONCAT(episode.c12) as additional column:
select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
if(count(distinct `episode`.`c12`),
count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`,
`GROUP_CONCAT(episode.c12)` as `Seasons`
from
((((`tvshow`
left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
group by `tvshow`.`idShow`
having (count(`episode`.`c12`) > 0)
See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat for the documentation of this MySQL specific function.
I use the following MySQL to return a list of posts and their corresponding comments.
SELECT *
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
WHERE forum_qa_parent_id = $forum_qa_id
If I run
$data['num_answers'] = $query->num_rows();
This allows me to get the number of returned rows and pass the array to my controller and view.
But this is returning all rows (posts + comments). So if 1 post has 10 comments, it returns 10.
How could I have this query count only the number of posts (ie, returning 1) not including the subquery?
Each post has a unique id saved in forum_qa.forum_qa_id
Each comment has a unique id saved in forum_cm.forum_cm_id.
Thanks for helping -- will post more code if needed.
Not the fastest, but you are not restricted in using GROUP BY:
SELECT *,
(SELECT COUNT(*) FROM forum_qa WHERE forum_qa_parent_id = $forum_qa_id) Cnt
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
WHERE forum_qa_parent_id = $forum_qa_id
You can run another query or add one more column (with an independent subquery) in the result set:
SELECT *
, ( SELECT COUNT(*)
FROM forum_qa
WHERE forum_qa_parent_id = $forum_qa_id
) AS cntPosts
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
WHERE forum_qa_parent_id = $forum_qa_id
COUNT(DISTINCT forum_qa.forum_qa_id)
COUNT(DISTINCT col_name) counts the distinct post ids. This should equal the number of posts.