I would like to SUM the foglalas_mennyiseg field in the foglalas table.
With the code below, i get this error: Notice: Undefined index: foglalt
What am i doing wrong?
$sql =
"
SELECT
rendeles.rendeles_id,
rendeles.rendeles_gyarto,
rendeles.rendeles_termek,
rendeles.rendeles_mennyiseg,
rendeles.rendeles_szam,
rendeles.rendeles_status,
rendeles.rendeles_created,
rendeles.rendeles_visszaig,
gyarto.gyarto_nev,
termek.termek_nev,
termek.termek_egyseg,
(SELECT SUM(foglalas.foglalas_mennyiseg) AS foglalt FROM foglalas
WHERE foglalas.foglalas_rendeles_id = rendeles.rendeles_id)
FROM rendeles
LEFT JOIN gyarto ON rendeles.rendeles_gyarto = gyarto.gyarto_id
LEFT JOIN termek ON rendeles.rendeles_termek = termek.termek_id
ORDER BY rendeles_id DESC LIMIT $actual, $row_per_page
";
You need your name after your subquery
....
( SELECT SUM(foglalas.foglalas_mennyiseg)
FROM foglalas
WHERE foglalas.foglalas_rendeles_id = rendeles.rendeles_id
) AS foglalt
....
Related
I want to use mysql's "JOIN"
I want to group rows by "date_text" where "tokenIdx" is "1001" and "datetime_unix" is the highest value.
Is my code wrong?
SELECT `A.idx`
FROM `data_candle_h1` 'A'
JOIN
(
SELECT `date_text`, MAX(`datetime_unix`) AS 'datetime_unix'
FROM `data_candle_h1`
WHERE `tokenIdx` = '1002'
GROUP BY `date_text`
) 'B'
ON `A.datetime_unix` = `B.datetime_unix`
WHERE `A.tokenIdx` = '1002'
Your query is syntactically perfect. Just remove single quotes('') around table aliases (A and B). I have corrected it. Please check this out.
SELECT `A.idx`
FROM `data_candle_h1` A
JOIN
(
SELECT `date_text`, MAX(`datetime_unix`) AS 'datetime_unix'
FROM `data_candle_h1`
WHERE `tokenIdx` = '1002'
GROUP BY `date_text`
) B
ON `A.datetime_unix` = `B.datetime_unix`
WHERE `A.tokenIdx` = '1002'
I am trying to update two columns within a table from a select statment in MySQL 5.7.
The error I get is "invalid use of group function"
Stmt:
UPDATE
catalog mpc
JOIN
reviews mpr ON mpr.merchant_id = mpc.MERCHANT_ID and mpr.sku = mpc.ARTICLE_ID
SET
mpc.RATING = avg(mpr.rating),
mpc.RATINGS = count(mpr.rating)
WHERE
mpr.MERCHANT_ID = 1
AND mpr.sku = '133';
It looks about right to me, what could be the problem here?
You must aggregate first in reviews and then join to catalog:
UPDATE catalog mpc
INNER JOIN (
SELECT merchant_id, sku, AVG(rating) avg_rating, COUNT(rating) count_rating
FROM reviews
WHERE merchant_id = 1 AND sku = '133'
GROUP BY merchant_id, sku
) mpr ON mpr.merchant_id = mpc.MERCHANT_ID and mpr.sku = mpc.ARTICLE_ID
SET mpc.RATING = mpr.avg_rating,
mpc.RATINGS = mpr.count_rating
I have this query:
$query = '
SELECT * FROM "rdm_item"
LEFT JOIN "rdm_tag" ON "rdm_item"."aff_id" = "rdm_tag"."aff_type_id"
WHERE "rdm_item"."aff_publish" = 4
ORDER BY "rdm_item"."aff_id"
DESC LIMIT 0,12';
and tables:
rdm_item columns: aff_id/aff_publish/...
rdm_tag columns: aff_id/type_id/...
I could get results.
But for $result[0]['aff_id'] I expect rdm_item.aff_id but it return rdm_tag.aff_id
what's my problem?
You need to specify the columns you want from the select, prefixing them with the table name. For example:
$query = '
SELECT rdm_item.aff_id FROM "rdm_item"
LEFT JOIN "rdm_tag" ON "rdm_item"."aff_id" = "rdm_tag"."aff_type_id"
WHERE "rdm_item"."aff_publish" = 4
ORDER BY "rdm_item"."aff_id"
DESC LIMIT 0,12';
If you have duplicate column names in two tables (i.e. aff_id) use an identifier, e.g.
$query = '
SELECT rdm_item.aff_id as rdm_item_aff_id, rdm_tag.aff_id as rdm_tag_aff_id FROM "rdm_item"
LEFT JOIN "rdm_tag" ON "rdm_item"."aff_id" = "rdm_tag"."aff_type_id"
WHERE "rdm_item"."aff_publish" = 4
ORDER BY "rdm_item"."aff_id"
DESC LIMIT 0,12';
You can then refer to the correct column with something like result[0]['rdm_item_aff_id'].
I'm having trouble adding a condition on aliases is_paid, is_overdue and is_outstanding in the following query:
SELECT r.doc_number,
r.doc_date,
r.due_date,
r.currency,
r.amount,
r.vat,
r.vatammount,
(r.amount + r.vatammount) final_amount,
r.currency,
b.boq_id,
b.boq_comp_id,
b.boq_client_id,
b.boq_agency,
b.boq_date,
b.boq_orders,
b.receivable_id,
c.comp_name,
crm.`cn-name-first`,
crm.`cn-name-last`,
bi.inv_path,
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id) total_amount_received,
IF (r.amount + r.vatammount =
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_paid,
IF (CURRENT_DATE >= r.due_date
AND r.amount + r.vatammount !=
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_overdue,
IF (r.due_date < CURRENT_DATE
AND r.amount + r.vatammount !=
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_outstanding
FROM receivables r
LEFT JOIN boq b ON b.receivable_id = r.id
LEFT JOIN boq_invoices bi ON bi.inv_boq_id = b.boq_id
LEFT JOIN comp_companies c ON c.comp_id = b.boq_comp_id
LEFT JOIN crm_contacts crm ON crm.contact_id = b.boq_client_id
WHERE r.status = 'active'
AND r.doc_type = 'inv'
AND b.boq_status = 'active'
AND is_paid = '1'
ORDER BY r.doc_date DESC LIMIT 10
Is there any way to modify this query and to make it possible to add a condition on those three aliases?
use alias in where condition .. is not allowed . because .is not possibile
the query code is evaluted based on a specified order .. starting from FROM then
WHERE clause and last the SELECT and the column alias so .. when the where is performed the column alias is not available at the query
You could try with an having condition because having work on the result of the query and not on the raw rows value .. (this could have effect on performance ..because all the query is performed and only the result is filtered by having)
SELECT dieet.snr, soort.nsnaam FROM soort
JOIN dieet ON soort.snr = dieet.snr
JOIN voedsel ON dieet.voednr = voedsel.voednr
WHERE voedsel.voednr = 22
GROUP BY dieet.snr
HAVING COUNT(*) = 1 ;
the where condition messes up my output, what am i doing wrong
I suspect that you are looking for "snr"s that have exactly one record that is a "voednr = 22". Your query is getting all "snr"s that have exactly one such "voednr", along with other "voednr"s.
Let me suggest the following query:
SELECT dieet.snr, soort.nsnaam
FROM soort
JOIN dieet ON soort.snr = dieet.snr
JOIN voedsel ON dieet.voednr = voedsel.voednr
GROUP BY dieet.snr
HAVING COUNT(*) = 1 and max(voedsel.voednr) = 22
This will get you the rows with the one and only 22.