conditional sql query for range of results - mysql

I have a query that I want to show results differently depending on a specific field. For example, I am using
$result = $wpdb->get_results( "SELECT `b`.`company` AS `company`,`bp`.`material` AS `material`,
if(((`bp`.`cost` * 1.2) < `ls`.`maximumbid`),(ROUND(`bp`.`cost` * 1.2,2)),ROUND(`bp`.`cost`,2)) AS `newcost`
from (((`windows_brands_products` `bp` left join `windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`)))
join `Windows_last_submissions` `ls`) join `windows_materials` `wm`)
where ((`bp`.`width` = round(`ls`.`width`,0))
and (`bp`.`height` = round(`ls`.`height`,0))
and (`bp`.`material` = `wm`.`name`)
and (`bp`.`type` = `ls`.`type`)
and if((`ls`.`minimumbid` <> '0.00'),(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),(`bp`.`cost` <= `ls`.`maximumbid`)))
ORDER BY b.company ASC");
The results I want are if the row ls.type = 'Fixed/Picture', I want bp.width = round(ls.width,-1) and still as I have it for any other ls.type
I should be getting correct results from:
select `b`.`company` AS `company`,`bp`.`material` AS `material`,if(((`bp`.`cost` * 1.2) < `ls`.`maximumbid`),round((`bp`.`cost` * 1.2),2),round(`bp`.`cost`,2)) AS `newcost` from (((`windows_brands_products` `bp` left join `windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`))) join `Windows_last_submissions` `ls`) join `windows_materials` `wm`)
where if((`ls`.`type` = 'Fixed/Picture'),(`bp`.`width` = round(`ls`.`width`,-2),(`bp`.`width` = round(`ls`.`width`,0)) and (`bp`.`height` = round(`ls`.`height`,0)) and (`bp`.`material` = `wm`.`name`) and (`bp`.`type` = `ls`.`type`) and if((`ls`.`minimumbid` <> '0.00'),(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),(`bp`.`cost` <= `ls`.`maximumbid`))) order by `b`.`company`
but it keeps giving me syntax errors as I try to correct the query which I'm not succeeding.
The point is retrieve a broader range of results if the type is 'Fixed/Picture'
Thank you,
Casey

Related

Error near Group and order by while converting ORACLE query to Aurora DB MYSQL query

iam trying to convert following ORACLE query into Aurora DB MYSQL query.
Changed few oracle functions to mysql functions.
SELECT MIN(TP.TP_ID) TP_ID,
TC_GRP.TC_GRP_NB TC_GRP_NB,
MIN(TC.TC_ID) TC_ID,
MIN(TC.FAC_ID) FAC_ID,
LISTAGG(TC_GST.TXN_IDVL_PTY_ID, ',') WITHIN GROUP (ORDER BY TC_GST.TXN_IDVL_PTY_ID) AS TXN_IDVL_PTY_ID
FROM RES_MGMT.TP
INNER JOIN RES_MGMT.TPS
ON TPS.TP_ID = TP.TP_ID
INNER JOIN RES_MGMT.TC_GRP
ON TC_GRP.TPS_ID = TPS.TPS_ID
INNER JOIN RES_MGMT.TC
ON (TC.TC_GRP_NB = TC_GRP.TC_GRP_NB AND
TC.TC_TYP_NM = 'AccommodationComponent' AND
TC.TRVL_STS_NM = 'Booked' AND TC.UPGRD_TC_ID IS NULL)
INNER JOIN RES_MGMT.TC_GST
ON TC_GST.TC_ID = TC.TC_ID
WHERE TC_GRP.ADV_INTERNT_CHKIN_IN = 'Y'
AND TPS.SRC_ACCT_CTR_ID = 2
AND TC.FAC_ID in (80010383)
AND TRUNC(TC.TC_STRT_DTS) = TO_DATE('20210930', 'yyyyMMdd')
AND EXISTS (SELECT *
FROM RES_MGMT.TC TC1
WHERE TC1.TC_GRP_NB = TC.TC_GRP_NB
AND TC1.TC_TYP_NM = 'AdmissionComponent'
AND TC1.TRVL_STS_NM = 'Booked')
GROUP BY TC_GRP.TC_GRP_NB;
MySQLQuery has modified as follows:
SELECT MIN(TP.TP_ID) TP_ID,
TC_GRP.TC_GRP_NB TC_GRP_NB,
MIN(TC.TC_ID) TC_ID,
MIN(TC.FAC_ID) FAC_ID,
LISTAGG(TC_GST.TXN_IDVL_PTY_ID, ',') WITHIN GROUP (ORDER BY TC_GST.TXN_IDVL_PTY_ID) AS TXN_IDVL_PTY_ID
FROM RES_MGMT.TP
INNER JOIN RES_MGMT.TPS
ON TPS.TP_ID = TP.TP_ID
INNER JOIN RES_MGMT.TC_GRP
ON TC_GRP.TPS_ID = TPS.TPS_ID
INNER JOIN RES_MGMT.TC
ON (TC.TC_GRP_NB = TC_GRP.TC_GRP_NB AND
TC.TC_TYP_NM = 'AccommodationComponent' AND
TC.TRVL_STS_NM = 'Booked' AND TC.UPGRD_TC_ID IS NULL)
INNER JOIN RES_MGMT.TC_GST
ON TC_GST.TC_ID = TC.TC_ID
WHERE TC_GRP.ADV_INTERNT_CHKIN_IN = 'Y'
AND TPS.SRC_ACCT_CTR_ID = 2
AND TC.FAC_ID in (80010385)
AND DATE(TC.TC_STRT_DTS) = STR_TO_DATE('20210930', '%Y-%m-%d')
AND EXISTS (SELECT *
FROM RES_MGMT.TC TC1
WHERE TC1.TC_GRP_NB = TC.TC_GRP_NB
AND TC1.TC_TYP_NM = 'AdmissionComponent'
AND TC1.TRVL_STS_NM = 'Booked')
GROUP BY TC_GRP.TC_GRP_NB;
Iam getting mysql syntax error near WITHIN GROUP.
Kindly suggest what need to be changed.

OR condition with Joins using cakephp and mysql

Iam searching a keyword in solr and it returns matched resume id's,when I got the result from solr I am searching those documents and getting matched job seekers from my database.
Now my question is have to get the record if it match in documents or in job seekers skills i.e job_seekers.skills column how to write the query..
Here is my existing query..
SELECT *
FROM `job_seekers` AS `JobSeeker`
LEFT JOIN `job_seeker_documents` AS `JobSeekerDocument` ON (`JobSeeker`.`id` = `JobSeekerDocument`.`job_seeker_id` AND `doc_attachment` IN ('1457448773Jan.doc', '1457448764Eric.doc', 'Vijal_Chokshi_Profile.doc', 'Deborah_Project manager_Profile..docx'))
LEFT JOIN `config_work_authorizations` AS `ConfigWorkAuthorization` ON (`JobSeeker`.`work_authorization` = `ConfigWorkAuthorization`.`id`)
LEFT JOIN `employees` AS `Employee` ON (`Employee`.`job_seeker_id` = `JobSeeker`.`id`)
WHERE `JobSeeker`.`company_id` = 11 AND `JobSeeker`.`skills` = 'java' AND `JobSeeker`.`bench_status` IN (0, 1) AND (select count(*) from employee_jobs where `employee_jobs`.`employee_id`=`Employee`.`id` and `employee_jobs`.`job_status` =1 ) = 0 GROUP BY `JobSeeker`.`id` HAVING (COUNT(`JobSeekerDocument`.`id`) > 0 );
you can write or condition in brackets i.e. () like
(`JobSeeker`.`skills` = 'java' OR `JobSeekerDocument`.`skills` = 'java')
In your query
SELECT *
FROM `job_seekers` AS `JobSeeker`
LEFT JOIN `job_seeker_documents` AS `JobSeekerDocument` ON (`JobSeeker`.`id` = `JobSeekerDocument`.`job_seeker_id` AND `doc_attachment` IN ('1457448773Jan.doc', '1457448764Eric.doc', 'Vijal_Chokshi_Profile.doc', 'Deborah_Project manager_Profile..docx'))
LEFT JOIN `config_work_authorizations` AS `ConfigWorkAuthorization` ON (`JobSeeker`.`work_authorization` = `ConfigWorkAuthorization`.`id`)
LEFT JOIN `employees` AS `Employee` ON (`Employee`.`job_seeker_id` = `JobSeeker`.`id`)
WHERE `JobSeeker`.`company_id` = 11
AND (`JobSeeker`.`skills` = 'java' OR `JobSeekerDocument`.`skills` = 'java')
AND `JobSeeker`.`bench_status` IN (0, 1)
AND (select count(*) from employee_jobs where `employee_jobs`.`employee_id`=`Employee`.`id` and `employee_jobs`.`job_status` =1 ) = 0
GROUP BY `JobSeeker`.`id`
HAVING (COUNT(`JobSeekerDocument`.`id`) > 0 );

Golang GORP - No error, no results

dbCount, er2 = dbmap.SelectInt("SELECT COUNT(*) AS N FROM (SELECT av.id FROM Table av LEFT JOIN Table2 dt ON dt.`app_id` = av.`app_id` LEFT JOIN Table3 a ON a.`id` = av.`app_id` WHERE dt.`user_id` IN (?) OR a.`org_id` IN (?) GROUP BY av.id LIMIT ? OFFSET ?) tmp", user_id, org_ids, limit, offset)
user_id = 1000
org_ids = 13444,12444,10333
limit = 25
offset = 0
Any idea why this returns no error or results in GO but when run directly on the database returns 1 result? There are no errors either.

MySQL QUERY runs on localhost, but does not on server

I have this QUERY:
select
a.*
from
mt_proyecto a,
mt_mockup b,
mt_diseno c,
mt_modulo d
where
a.estado = 'A' and
(
(b.encargado = '1' and b.idproyecto = a.idmtproyecto) or
(c.encargado = '1' and c.idproyecto = a.idmtproyecto) or
(d.encargado = '1' and d.idproyecto = a.idmtproyecto)
)
group by
a.idmtproyecto
order by a.finalizado asc, a.feccrea desc
Result:
Then, I run the same code on server with the same database:
Is there any problem with the query?
It seems that the query is running correctly on your server, and returning no rows. Please make sure you have the same table contents on your local machine and your server.
Other things:
Pro tip: never use SELECT * or SELECT table.* in software. Always enumerate the columns you want in your result set.
Unless you use GROUP BY with aggregate functions like SUM() or `COUNT(), and naming the correct columns from the result set, it returns unpredictable results. Read this. http://dev.mysql.com/doc/refman/5.1/en/group-by-extensions.html
I solved with this QUERY:
select
a.*
from
(
mt_proyecto a
left join mt_mockup b on
b.idproyecto = a.idmtproyecto
left join mt_diseno c on
c.idproyecto = a.idmtproyecto
left join mt_modulo d on
d.idproyecto = a.idmtproyecto
left join mt_integracion e on
e.idproyecto = a.idmtproyecto
left join mt_pruebas_internas f on
f.idproyecto = a.idmtproyecto
)
where
a.estado = 'A' and
(
(a.idmtproyecto = b.idproyecto and
b.encargado = '1' ) or
(a.idmtproyecto = c.idproyecto and
c.encargado = '1' ) or
(a.idmtproyecto = d.idproyecto and
d.encargado = '1' ) or
(a.idmtproyecto = e.idproyecto and
e.encargado = '1' ) or
(a.idmtproyecto = f.idproyecto =
f.encargado = '1' )
)
group by a.idmtproyecto
order by
a.finalizado asc, a.feccrea desc
Thanks everyone for answer me. I'll do your suggestions .

Strange: MySQL Left Join, "unknown column"; the same leftjoin doesn't work in multiple-table query

I'm trying to run a select query, which is the following:
SELECT * FROM pages, c_item_category cc
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id)
WHERE (pages.page_id = cc.item_id AND cc.cat_id = 7)
AND (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10
But I get this error: Unknown column 'pages.page_id' in 'on clause'
Why is that? I can confirm that I have a column named 'page_id' in 'pages' :)
But, when I do a single-table query with the same left joins, it works well:
SELECT * FROM pages
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id)
WHERE (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10
What is my mistake in the first query? :\
Try it this way instead.
SELECT *
FROM pages
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id),
c_item_category cc
WHERE (pages.page_id = cc.item_id AND cc.cat_id = 7)
AND (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10