I have the following query and I am joining tasks_triggers_evidence.
I want to get the value of the field evidence_accepted.
But unfortunately it's excluding all the rows whenever there are no matches. I want it to return the results, but with a NULL for evidence_accepted.
So join to grab data, but not exclude any results basically. How do I do that?
SELECT o.task_id, o.task_link, SUM(t.trigger_conversions), SUM(t.trigger_reward) AS reward, e.evidence_accepted
FROM tasks AS o
INNER JOIN advertisers AS a
ON a.advertiser_id = o.task_advertiser_id
INNER JOIN tasks_triggers AS t
ON o.task_id = t.trigger_task_id
LEFT JOIN tasks_triggers_evidence AS e
ON e.evidence_trigger_id = t.trigger_id
WHERE o.task_approved = 1 AND o.task_deleted = 0 AND o.task_paused = 0 AND t.trigger_removed = 0 AND e.evidence_account_id = ?
AND a.advertiser_balance > 0
GROUP BY o.task_id
ORDER BY reward DESC
Write condition e.evidence_account_id = ? not in WHERE section, but in LEFT JOIN:
SELECT o.task_id, o.task_link, SUM(t.trigger_conversions), SUM(t.trigger_reward) AS reward, e.evidence_accepted
FROM tasks AS o
INNER JOIN advertisers AS a
ON a.advertiser_id = o.task_advertiser_id
INNER JOIN tasks_triggers AS t
ON o.task_id = t.trigger_task_id
LEFT JOIN tasks_triggers_evidence AS e
ON e.evidence_trigger_id = t.trigger_id AND e.evidence_account_id = ?
WHERE o.task_approved = 1
AND o.task_deleted = 0
AND o.task_paused = 0
AND t.trigger_removed = 0
AND a.advertiser_balance > 0
GROUP BY o.task_id
ORDER BY reward DESC
Related
I have this query which i believe can be optimized:
SELECT floors.id, floors.floor FROM floors
WHERE floors.societies_id = 1
AND floors.status = 'Y'
AND floors.id NOT IN (
SELECT DISTINCT(floors.id) FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
INNER JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
)
Is this query fine to use or there it can be improved..?
It looks like you want to get all floors that aren't present in resident_floors. For this we can left join RF in and ask for only rows where the join failed resulting in a null in RF:
SELECT floors.* FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
LEFT JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
AND resident_floors.floors_id IS NULL
SELECT `mpeda_fish`.`id`, `mpeda_fish`.`fish` as analysis, sum(mpeda_fishdetails.quantity) as qty
FROM (`mpeda_fishdetails`)
INNER JOIN `mpeda_scientificfish` ON `mpeda_scientificfish`.`id` = `mpeda_fishdetails`.`scientificfish`
INNER JOIN `mpeda_fish` ON `mpeda_fish`.`id` = `mpeda_scientificfish`.`fish`
INNER JOIN `mpeda_fishcatch` ON `mpeda_fishcatch`.`id` = `mpeda_fishdetails`.`fishcatch`
INNER JOIN `mpeda_harbour` ON `mpeda_harbour`.`id` = `mpeda_fishcatch`.`harbour`
WHERE `mpeda_fishcatch`.`status` = 1
ORDER BY `mpeda_fishdetails`.`id` ASC
this query gets 2 columns null value and one column gets data inside why?
You use the SUM() function. In order to get meaningful results you should have a group by clause.
SELECT `mpeda_fish`.`id`, `mpeda_fish`.`fish` as analysis, sum(mpeda_fishdetails.quantity) as qty
FROM (`mpeda_fishdetails`)
INNER JOIN `mpeda_scientificfish` ON `mpeda_scientificfish`.`id` = `mpeda_fishdetails`.`scientificfish`
INNER JOIN `mpeda_fish` ON `mpeda_fish`.`id` = `mpeda_scientificfish`.`fish`
INNER JOIN `mpeda_fishcatch` ON `mpeda_fishcatch`.`id` = `mpeda_fishdetails`.`fishcatch`
INNER JOIN `mpeda_harbour` ON `mpeda_harbour`.`id` = `mpeda_fishcatch`.`harbour`
WHERE `mpeda_fishcatch`.`status` = 1
GROUP BY `mpeda_fish`.`id`, `mpeda_fish`.`fish`
ORDER BY `mpeda_fishdetails`.`id` ASC
i have a huge query:
SELECT stories.*
FROM stories stories
JOIN (SELECT tstories.uid
FROM stories tstories
JOIN tours_stories_mm mmt
ON tstories.uid = mmt.uid_foreign
JOIN tours tours
ON tours.uid = mmt.uid_local
JOIN tours_countries_rel tourcountryrel
ON tours.uid = tourcountryrel.tourid
JOIN countries tcountries
ON tcountries.uid = tourcountryrel.countryid
WHERE tcountries.uid = ?
AND tours.deleted = 0
AND tours.hidden = 0
AND tstories.deleted = 0
AND tstories.hidden = 0) as tourstories
JOIN (SELECT cstories.uid
FROM stories cstories
JOIN individualtourcomponents_stories_mm mmc
ON (cstories.uid = mmc.uid_foreign)
JOIN individualtourscomponents components
ON (components.uid = mmc.uid_local)
JOIN individualtourcomponents_countries_rel componentcountryrel
ON (components.uid = componentcountryrel.componentid)
JOIN countries ccountries
ON (ccountries.uid = componentcountryrel.countryid)
WHERE ccountries.uid = ?
AND components.deleted = 0
AND components.hidden = 0
AND cstories.deleted = 0
AND cstories.hidden = 0) as componentstories
WHERE stories.uid = componentstories.uid
OR stories.uid = tourstories.uid
GROUP BY stories.uid`
each of the subqueries work for themselves and if both subqueries have the same result, i get an result on the whole query. If only one subquery has a result i get nothing. Simplyfied it could be written like that:
SELECT stories.*
FROM stories
JOIN (SELECT * FROM table_a WHERE x=5) as table_a
JOIN (SELECT * form table_b where x=5) as table_b
WHERE stories.uid = table_a.uid OR stories.uid = table_b.uid
GROUP BY stories.uid
What am i doing wrong?
It seems like you should be using an OUTER JOIN instead of INNER JOIN to join your source tables.
Using the simplified query example, it might look like this:
SELECT stories.*
FROM stories
LEFT OUTER JOIN (SELECT * FROM table_a WHERE x=5) as table_a
ON stories.uid = table_a.uid
LEFT OUTER JOIN (SELECT * form table_b where x=5) as table_b
ON stories.uid = table_b.uid
GROUP BY stories.uid
UPDATE
Based on your comment it sounds like you actually want to get the distinct union of the the two sub queries.
If that's the case, you could modify your original query like this:
(SELECT tstories.*
FROM stories tstories
JOIN tours_stories_mm mmt
ON tstories.uid = mmt.uid_foreign
JOIN tours tours
ON tours.uid = mmt.uid_local
JOIN tours_countries_rel tourcountryrel
ON tours.uid = tourcountryrel.tourid
JOIN countries tcountries
ON tcountries.uid = tourcountryrel.countryid
WHERE tcountries.uid = ?
AND tours.deleted = 0
AND tours.hidden = 0
AND tstories.deleted = 0
AND tstories.hidden = 0
)
UNION DISTINCT
(SELECT cstories.*
FROM stories cstories
JOIN individualtourcomponents_stories_mm mmc
ON (cstories.uid = mmc.uid_foreign)
JOIN individualtourscomponents components
ON (components.uid = mmc.uid_local)
JOIN individualtourcomponents_countries_rel componentcountryrel
ON (components.uid = componentcountryrel.componentid)
JOIN countries ccountries
ON (ccountries.uid = componentcountryrel.countryid)
WHERE ccountries.uid = ?
AND components.deleted = 0
AND components.hidden = 0
AND cstories.deleted = 0
AND cstories.hidden = 0
)
I've got this query but the result is wrong.
How can I use the min() statement and the Group by Statement so that I will get for each AthletenID the lowest DiszOrder?
Select
ar_Leistungen.`AthletenID`,
ar_Leistungen.`Leistung`,
ar_Leistungen.`Disziplin`,
ar_Leistungen.`Klasse`,
min(ar_Leistungen.`DiszOrder`),
ar_Athleten.`Vorname`,
ar_Athleten.`Jahrgang`,
ar_Wettkampf.`Wettkampfdatum`
from
ar_Leistungen,
ar_Athleten,
ar_Wettkampf
Where
ar_Athleten.ID = ar_Leistungen.AthletenID and
ar_Leistungen.WettkampfID = ar_Wettkampf.ID and
ar_Leistungen.`Disziplin` = '100' and
ar_Leistungen.`Leistung` > 0 and
(ar_Athleten.`Jahrgang` = '1995' or ar_Athleten.`Jahrgang` = '1994') and
ar_Wettkampf.`Wettkampfdatum` LIKE '%2013%'
Group By
AthletenID
Order by
DiszOrder Desc
Limit
0, 100
You can have a subquery which separately gets the lowest DiszOrder for each AthletenID and join it with the other table so you can freely get the other value of the columns.
SELECT a.AthletenID,
a.Leistung,
a.Disziplin,
ar_Leistungen.Klasse,
a.DiszOrder),
b.Vorname,
b.Jahrgang,
c.Wettkampfdatum
FROM ar_Leistungen a
INNER JOIN ar_Athleten b
ON b.ID = a.AthletenID
INNER JOIN ar_Wettkampf c
ON a.WettkampfID = c.ID
INNER JOIN
(
SELECT AthletenID, MIN(DiszOrder) DiszOrder
FROM ar_Leistungen
GROUP BY AthletenID
) d ON a.AthletenID = d.AthletenID AND
a.DiszOrder = d.DiszOrder
WHERE a.Disziplin = '100' AND
a.Leistung > 0 AND
(b.Jahrgang IN ('1995', '1994'))
SELECT invoices.number_formatted, SUM(invoices_elements.netto)
FROM invoices
LEFT JOIN invoices_elements ON invoices_elements_code_invoices_id = invoices_id
WHERE invoices_enable = 1
AND invoices_elements_enable = 1
GROUP BY invoices_elements_code_invoices_id
If table "invoices_elements" doesn't have any rows with "invoices_elements_enable = 1" this query return NULL - but i want "number formatted". So i do this:
SELECT SUM(netto)
FROM (invoices)
LEFT JOIN (SELECT * FROM invoices_elements WHERE invoices_elements_enable = 1) ON invoices_elements_code_invoices_id = invoices_id
WHERE invoices_enable = 1
GROUP BY invoices_elements_code_invoices_id
... and this of coz works. But - is better way to do it?
You can do
SELECT SUM(netto)
FROM invoices
LEFT JOIN invoices_elements
ON invoices_elements_code_invoices_id = invoices_id
AND invoices_elements_enable = 1
WHERE invoices_enable = 1
GROUP BY invoices_elements_code_invoices_id
Note the restriction invoices_elements_enable = 1 is in the ON clause to avoid converting the query into an inner join.