MySQL WHERE With IF Condition - mysql

can anyone help me on why the WHERE clause if not functioning? And what is the alternative solution on it?
SELECT jq.jobid
FROM jobqueue jq
LEFT JOIN taskslogs tl
ON jq.taskqueueid=tl.taskqueueid
LEFT JOIN (SELECT p1.taskID,p1.processingType
FROM projecttask p
LEFT JOIN projecttask p1
ON p.sequenceNo=p1.nextTaskSequence
AND p.projectID=p1.projectID
WHERE p.taskID=paramTaskID) prevTask
ON tl.taskID=prevTask.taskID
WHERE IF(prevTask.processingType='BATCH',jq.batchid!=0,IF(prevTask.processingType='DOCGROUP',jq.documentgroupid!=0,TRUE))
GROUP BY jq.jobid

Maybe something like this:
WHERE
(prevTask.processingType='BATCH' AND jq.batchid!=0)
OR (prevTask.processingType='DOCGROUP' AND jq.documentgroupid!=0)

I don't think you can use IF statements in a query. That is more for procedures.
You should probably look at using case statements.
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

Related

Nesting COUNT in statement with JOIN

Really trying to figure out, why SQL query doesnt go through. I assume the structure is a bit wrong, but cant figure out where exactly. The references to tables are all correct.
SELECT tap_questionnaires.id,
tap_questionnaires.NAME,
tap_questionnaires.active,
tap_useranswers_ip.questionnaire_id,
Count(tap_useranswers_ip.ip)
FROM tap_questionnaires
LEFT JOIN tap_useranswers_ip
ON tap_questionnaires.id = tap_useranswers_ip.questionnaire_id
WHERE author_email = admin#admin.com
If you use count you need to use group by for the other columns in your select clause.
SELECT TAP_questionnaires.id, TAP_questionnaires.name, TAP_questionnaires.active, TAP_useranswers_ip.questionnaire_id, COUNT(TAP_useranswers_ip.ip) FROM TAP_questionnaires LEFT JOIN TAP_useranswers_ip on TAP_questionnaires.id=TAP_useranswers_ip.questionnaire_id WHERE author_email="admin#admin.com"
group by TAP_questionnaires.id, TAP_questionnaires.active
I think TAP_questionnaires.name it's not necessary because I suppose it depends on TAP_questionnaires.id. TAP_useranswers_ip.questionnaire_id is the same value as TAP_questionnaires.id
Hope that helps!
I think this version is clearer:
SELECT q.id, q.name, q.active, COUNT(a.ip)
FROM TAP_questionnaires q LEFT JOIN
TAP_useranswers_ip a
ON on q.id = a.questionnaire_id
WHERE author_email = 'admin#admin.com'
GROUP BY q.id, q.name, q.active;
Notes:
You need a GROUP BY.
You need single quotes around the string constant.
Table aliases make the query easier to write and to read.
There is no reason to include a.questionnaire_id. You already have q.id.

Magento - SQL query programmatically

I have a SQL query that i would like to write with Magento's collection methods but i don't know how to.
I know that i have to use the getSelect() and joinLeft() methods, but don't know how to put a select inside a left join.
The query is :
SELECT
p.photo_id,
p.photo_name,
s.step_id,
s.step_name
FROM Photo p
LEFT JOIN (
SELECT
photo_id, MAX(step_id) AS max_step_id
FROM photoStep
GROUP BY photo_id
) ps
ON ps.photo_id = p.photo_id
LEFT JOIN Steps s
ON s.step_id = ps.max_step_id
I think that your best option here would be to try to avoid using the subquery. So I would start by reworking the SQL query and then use the functions groupByField & addExpressionFieldToSelect.
->groupByField('photo_id')
->addExpressionFieldToSelect("max_step_id", 'MAX({{step_id}})', 'step_id')

MySQL joins where one field has surrounding tags to be stripped

I have two MySQL tables, articoli and tabtranslations, and the join between them should be trivial
SELECT CodArt, Translation FROM articoli LEFT JOIN tabtranslations ON articoli.CodArt = tabtranslations.Chiave
BUT, while articoli.CodArt ha simple strings (A001, BS15, etc..), field tabtranslations.Chiave is filled with surronding tags like <CODART>A001</CODART>, <CODART>BS15</CODART> thus overcomplicating joins - and I cannot modify it...
Well, is there a way I can solve this problem? Thanks
A quick and dirty soulution would be like this:
SELECT CodArt, Translation
FROM
articoli LEFT JOIN tabtranslations
ON CONCAT('<CODART>', articoli.CodArt, '</CODART>') = tabtranslations.Chiave
I don't know, but maybe this will work?
SELECT CodArt, Translation
FROM articoli
LEFT JOIN tabtranslations ON tabtranslations.Chiave LIKE '%' + articoli.CodArt + '%'
Just in case 'CODART' isn't the only possible tag, you may want to use REGEXP in the join predicate, like so:
select *
from codart c
left join tabtranslations t
on t.chiave REGEXP CONCAT('<.*>', c.codart, '</.*>');
Here is a sample fiddle for you to try it out: http://sqlfiddle.com/#!9/d6c04/1

MySQL COUNT on WHERE Statement

Pleas help me on why COUNT condition on WHERE statement commits an error, and how could i fix it?.
SELECT jq.taskqueueid,jq.jobid
FROM (SELECT p.taskID `curentTaskID`,
p.taskName `currentTaskName`,
p.processingType `currentProcessingType`,
p1.taskID `prevTaskID`,
p1.taskName `prevTaskName`,
p1.processingType `prevProcessingType`
FROM projecttask p
LEFT JOIN projecttask p1
ON p.sequenceNo=p1.nextTaskSequence
AND p.projectID=p1.projectID
WHERE p.taskID=18) task
INNER JOIN taskslogs tl
ON tl.taskID=task.`prevTaskID`
AND tl.statusDefinitionID=1
INNER JOIN jobqueue jq
ON tl.taskqueueid=jq.taskqueueid
WHERE COUNT(jq.taskqueueid)=COUNT(tl.taskqueueid)
to use an aggregate function like COUNT() you need to do a grouping of data, if you want to use it as a condition you can't use WHERE for this, since WHERE conditions are considered before aggregation. Use GROUP BY with HAVING instead. (see also http://dev.mysql.com/doc/refman/5.0/en/select.html )

MySQL query - multiple having statements not working

I'm trying to use the following query, and if it only has one having statement it works as expected. If I add the second having statement it does not work.
SELECT candidate.first_name,
candidate.last_name,
qualification.code,
property.value AS funding_band_value,
qualification.funding_band,
property.value AS qualification_level_value,
qualification.qualification_level_id
FROM candidate_qualification, candidate, qualification, property
WHERE candidate_qualification.candidate_id=candidate.id and
candidate_qualification.qualification_id=qualification.id
HAVING funding_band_value = (select property.value from property where qualification.funding_band=property.id) and
HAVING qualification_level_value = (select property.value from property where qualification.qualification_level_id=property.id)
Could someone explain why this doesn't work and how I should do this.
HAVING acts similarly to WHERE or GROUP BY. You reference it once to start using it and combine multiple statements with AND or OR operators. An in depth look at the query parser might give you a more explicit answer.
You don't need HAVING here, just use AND so it is part of your WHERE clause.
The subqueries are not necessary, those tables are already joined.
Something like this should be closer to what you want:
SELECT c.first_name,
c.last_name,
q.code,
p.value AS funding_band_value,
q.funding_band,
p.value AS qualification_level_value,
q.qualification_level_id
FROM candidate_qualification cq
INNER JOIN candidate c ON cq.candidate_id=c.id
INNER JOIN qualification q ON cq.qualification_id=q.id
INNER JOIN property p ON q.funding_band=p.id
and q.qualification_level_id=p.id