Portál AbcLinuxu, 10. května 2025 09:15
select P.cislo, P.numeric1, P.numeric2, P.string1, P.string2, P.data, coalesce(counter,0) as counter, kdy, S.pridal, S.skupina, S.prava, R.cislo, contract from relace R, spolecne S, polozka P left join (select R1.potomek as autor, count(P1.cislo) as counter, max(S1.vytvoreno) as kdy from polozka P1, relace R1, vlastnost V1, spolecne S1 where P1.typ=2 and P1.cislo=V1.predek and V1.typ_predka='P' and V1.typ='author' and R1.typ_potomka='P' and V1.hodnota=R1.cislo and S1.typ='P' and P1.cislo=S1.cislo and S1.vytvoreno < now() group by autor) A on A.autor=P.cislo left join (select SCS.pridal as uid, CT.numeric1 as contract from polozka SC, polozka CT, spolecne SCS where SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0 ) B on B.uid = P.numeric1 where P.typ=19 and R.typ_potomka=S.typ and R.potomek=S.cislo and S.typ='P' and S.cislo=P.cislo and P.numeric1 in (1,99,143)Trochu vysvetleni:
SELECT ... ORDER BY CT.cislo DESC LIMIT 1Ale to sa mi vidi prilis trivialne, asi som nepochopil otazku.
WHERE
subquery dát podmínku
CT.cislo = (SELECT MAX(CT.cislo) ...)
a zopakovat tam dotaz.(SELECT SCS.pridal as uid, CT.numeric1 as contract FROM polozka SC, polozka CT, spolecne SCS ,(SELECT MAX(CT.cislo) AS cfuj from polozka SC, polozka CT, spolecne SCS where SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0) AS tfuj WHERE SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0 AND CT.cislo = tfuj.cfuj GROUP BY tfuj.cfuj) AS BAle o kvalitě bych nehovořil (nebijte mě…)
SELECT SCS.pridal as uid, CT.numeric1 as contract FROM polozka SC, polozka CT, spolecne SCS ,(SELECT MAX(CT.cislo) AS cfuj from polozka SC, polozka CT, spolecne SCS where SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0) AS tfuj WHERE SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0 AND CT.cislo = tfuj.cfuj GROUP BY tfuj.cfuja kompletní dotaz:
select P.cislo, P.numeric1, P.numeric2, P.string1, P.string2, P.data, coalesce(counter,0) as counter, kdy, S.pridal, S.skupina, S.prava, R.cislo, contract from relace R, spolecne S, polozka P left join (select R1.potomek as autor, count(P1.cislo) as counter, max(S1.vytvoreno) as kdy from polozka P1, relace R1, vlastnost V1, spolecne S1 where P1.typ=2 and P1.cislo=V1.predek and V1.typ_predka='P' and V1.typ='author' and R1.typ_potomka='P' and V1.hodnota=R1.cislo and S1.typ='P' and P1.cislo=S1.cislo and S1.vytvoreno < now() group by autor) A on A.autor=P.cislo left join (SELECT SCS.pridal as uid, CT.numeric1 as contract FROM polozka SC, polozka CT, spolecne SCS ,(SELECT MAX(CT.cislo) AS cfuj from polozka SC, polozka CT, spolecne SCS where SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0) AS tfuj WHERE SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0 AND CT.cislo = tfuj.cfuj GROUP BY tfuj.cfuj) AS B on B.uid = P.numeric1 where P.typ=19 and R.typ_potomka=S.typ and R.potomek=S.cislo and S.typ='P' and S.cislo=P.cislo and P.numeric1 in (1,99,143)taky. Samozřejmně nad prázdnou db na verzi 5.0.67. No ono asi je něco ne úplně OK v datovém modelu, pokud to nutí dělat takovéto dotazy :), ale neznám, pozadí jak to vznikalo a ani jsem to nijak nestudoval, takže si netroufám hodnotit - jen pocit na základě, již druhého podobného dotazu.
Tak castecny uspech, pro jednoho uzivatele to ale vraci NULL. Trochu jsem to rozkouskoval / zkratil:
SELECT SCS.pridal as uid, CT.numeric1 as contract, CT.cislo FROM polozka SC, polozka CT, spolecne SCS WHERE SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0;
1;-1;147251
99;-1;147251
99;1;147269
SELECT MAX(CT.cislo) AS cfuj from polozka CT where CT.typ=30 and CT.numeric1<>0;
147269
Cely subselect vraci:
99;1
Jdu si hrat dal
GROUP BY tfuj.cfuj
- jen tak vařím…select P.cislo, P.numeric1, P.numeric2, P.string1, P.string2, P.data, coalesce(counter,0) as counter, kdy, S.pridal, S.skupina, S.prava, R.cislo, contract from relace R, spolecne S, polozka P left join (select R1.potomek as autor, count(P1.cislo) as counter, max(S1.vytvoreno) as kdy from polozka P1, relace R1, vlastnost V1, spolecne S1 where P1.typ=2 and P1.cislo=V1.predek and V1.typ_predka='P' and V1.typ='author' and R1.typ_potomka='P' and V1.hodnota=R1.cislo and S1.typ='P' and P1.cislo=S1.cislo and S1.vytvoreno < now() group by autor) A on A.autor=P.cislo left join (SELECT SCS.pridal as uid, CT.numeric1 as contract FROM polozka SC, polozka CT, spolecne SCS ,(SELECT MAX(CT.cislo) AS cfuj from polozka SC, polozka CT, spolecne SCS where SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0) AS tfuj WHERE SC.typ=31 and CT.typ=30 and SC.numeric1 = CT.cislo and SC.cislo=SCS.cislo and SCS.typ='P' and CT.numeric1<>0 AND CT.cislo = tfuj.cfuj) AS B on B.uid = P.numeric1 where P.typ=19 and R.typ_potomka=S.typ and R.potomek=S.cislo and S.typ='P' and S.cislo=P.cislo and P.numeric1 in (1,99,143)
Prepis jsem odladil, uz zase vse funguje, dokonce tento dotaz vraci vysledky, jak ma. Jen jsem chtel podle noveho sloupecku filtrovat a z nejakeho divneho duvodu si mysql stezuje, ze to nejde, ze sloupecek status nezna.
select P.cislo, P.numeric1, P.boolean1, P.string1, P.string2, P.data, coalesce(counter,0) as counter, kdy, S.pridal, S.skupina, S.prava, R.cislo, (select CT.boolean2 from polozka CT where CT.cislo=P.numeric2) as status from relace R join spolecne S on R.typ_potomka=S.typ and R.potomek=S.cislo join polozka P on S.typ='P' and S.cislo=P.cislo left join (select R1.potomek as autor, count(P1.cislo) as counter, max(S1.vytvoreno) as kdy from polozka P1, relace R1, vlastnost V1, spolecne S1 where P1.typ=2 and P1.cislo=V1.predek and V1.typ_predka='P' and V1.typ='author' and R1.typ_potomka='P' and V1.hodnota=R1.cislo and S1.typ='P' and P1.cislo=S1.cislo and S1.vytvoreno < now() group by autor) A on A.autor=P.cislo where P.typ=19 AND status=1 ORDER BY string2 ASC;
Chyba zni:
Unknown column 'status' in 'where clause'
Nedava i to smysl, nastudoval jsem mysql dokumentaci k subselectum a nepisou, ze by to nemelo jit. Dokazu filtrovat podle vsech sloupecku mimo tento ze subselectu. Proc?
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.