Autor Zpráva
kohl
Profil
Dobrý den,
napsal jsem dotaz, ale vyhazuje chybu "Unknown column 'num_l' in 'where clause'" a jiné řešení mě nenapadá. Potřeboval bych nasměrovat.

select 
edition_title,
(select count(lend_id) from lend inner join identifier on identifier_id=lend_identifier where identifier_edition=edition_id)as num_l,
(select count(lend_history_id) from lend_history inner join identifier on identifier_id=lend_history_identifier where identifier_edition=edition_id)as num_lh 
from edition inner join author on author.author_id=edition.edition_author 
where (num_l+num_lh)>0 
order by (num_l+num_lh) desc,edition_title 
limit 7

Děkuji za odpověď.
Joker
Profil
kohl:
Znamená to, že v SQL dotazu je použitý sloupec num_l, který v tabulce neexistuje.
Tori
Profil
kohl:
Místo where by muselo asi být having, když odkazujete na výsledek agregační funkce. Zkoušel jste to zapsat jako joiny, jestli to je nějak výrazně horší?

select edition_title, count(lend_id) + count(lend_history_id) cnt

from edition 
inner join author on author.author_id=edition.edition_author 
left join 
    (lend inner join identifier i1 on i1.identifier_id=lend_identifier) 
    on i1.identifier_edition=edition_id
left join 
    (lend_history inner join identifier i2 on i2.identifier_id=lend_history_identifier)
    on i2.identifier_edition=edition_id    

group by edition_title    
having cnt > 0 
order by cnt desc, edition_title 
limit 7
Píšu z hlavy, nemusí to být úplně správně syntakticky.
kohl
Profil
Děkuji za pomoc, ale na konec jsem to vyřešil tahle, je to asi 3x rychlejší.

SELECT edition_id, edition_title, year_all, author_all
FROM (

SELECT edition_id, edition_title, CONCAT(  '(', edition_year,  ')' ) AS year_all, CONCAT_WS(  ', ', CONCAT_WS(  ' ', author_surname, author_name ) , author_year ) AS author_all, (
(

SELECT COUNT( lend_id ) 
FROM lend
INNER JOIN identifier ON identifier_id = lend_identifier
WHERE identifier_edition = edition_id
) + ( 
SELECT COUNT( lend_history_id ) 
FROM lend_history
INNER JOIN identifier ON identifier_id = lend_history_identifier
WHERE identifier_edition = edition_id )
) AS num
FROM edition
INNER JOIN author ON author.author_id = edition.edition_author
ORDER BY num DESC , edition_title
LIMIT 7
) AS t1
WHERE num >0

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0