thesnarkie Posted February 19, 2010 Report Share Posted February 19, 2010 $query = "SELECT a.*, ag.group_id as groupId FROM account AS a LEFT JOIN account_to_group AS ag ON ( ag.account_id = a.id ) WHERE a.id = '123'"; Tabulā account_to_group šim account_id 123 atbilst vairāki rezultāti, bet kverijs parāda tikai pirmo. Ir kaut kā iespējams apvienot tos rezultātus vai arī kaut kā parādīt visus kaut kādā veidā? :D Quote Link to comment Share on other sites More sharing options...
anonīms Posted February 19, 2010 Report Share Posted February 19, 2010 nesaprotu, ko tev vajag? sum? Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted February 19, 2010 Author Report Share Posted February 19, 2010 account_to_group tabula: divas kolonnas: account_id un group_id. veicu kveriju ar to augstāk doto kodu, bet tabulā account_to_group ir vairākas rindas, kur account_id ir vienāds ar 123. bet mans tas kverijs parāda tikai pirmo no visām tām rindām, ko viņš piejoinoja. es gribu zināt vai kaut kā var visas tās rindas sataisīt arrayā vai vēl kā savādāk dabūt visas tās rindas kopā (ne saskaitīt).grūti paskaidrot.. Quote Link to comment Share on other sites More sharing options...
anonīms Posted February 19, 2010 Report Share Posted February 19, 2010 (edited) Cik sapratu, tad ir tā. account_to_group (account_id, group_id)123, 1 123, 5 151, 6 156, 1 123, 2 un tev vajag izvilkt visus tos, kur account_id = 123. Neredzu kapēc tur vajag vispār join.. Ja pareizi sapratu, tad tīrs selekts ar while. $query = mysql_query("SELECT group_id FROM account_to_group WHERE account_id = '123'"); while($q = mysql_fetch_assoc($query)) { echo $q['group_id'].'<br />'; } un izvadīs 15 2 Ja kļūdos, tad derētu kārtīgāk paskaidrot, ko tu gribi panākt. EDIT+ neredzu jēgu a.id == ag.account_id, ja reiz tālāk tāpat ir where a.group_id = 123' Edited February 19, 2010 by anonīms Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted February 19, 2010 Author Report Share Posted February 19, 2010 (edited) Jā, tā kā tu uzrakstīji ir, bet es sākumā skaidri neizteicos.. $query = "SELECT a.*, ag.group_id as groupId FROM account AS a LEFT JOIN account_to_group AS ag ON ( ag.account_id = a.id ) WHERE a.id = '123'"; //a.ip = '', a.email = '' Where var meklēt ne tikai pēc id, var būt arī e-pasts u.tml. Un, ja es, piemēram, meklēju pēc ip un ir vairāki rezultāti, tad tas while pie katra rezultāta, lai iegūtu visas tās rindas, man likās ne pārāk efektīvi, ja ir kāda 30 rezultāti, bet te es nonācu pie secinājuma, ka nezinu kā ar joiniem varētu kaut kā parādīt visas tās rindas, ja ne, tad vajadzējs ar kaut kādiem apkārtceļiem.. Edited February 19, 2010 by thesnarkie Quote Link to comment Share on other sites More sharing options...
2easy Posted February 20, 2010 Report Share Posted February 20, 2010 (edited) left joinam vajadzēja strādāt. snārkie, vēlreiz check your sql & data tikai NEpalaid šo kodu uz production servera, jo tur ir DROP TABLE, lai var refrešot :D function dbconn($sSrv, $sDb, $sUsr, $sPw) { // inicializē mysql connection @mysql_connect($sSrv, $sUsr, $sPw) or exit('<b>mysql_connect() error ' . mysql_errno() . ':</b> ' . mysql_error()); mysql_select_db($sDb) or exit('<b>mysql_select_db() error ' . mysql_errno() . ':</b> ' . mysql_error()); go('SET NAMES utf8'); } function go($sSql) { // izpilda mysql query $h = mysql_query($sSql) or exit('<b>mysql_query() error ' . mysql_errno() . ':</b> ' . mysql_error() . '<br /><b>query:</b> ' . substr($sSql, 0, 1000)); return $h; } function qs($s) {return is_null($s) ? 'NULL' : "'" . mysql_real_escape_string($s) . "'";} // query str - sagatavo tekstu (any string) ievietošanai mysql query function esql($sSql) { // echo sql & its returned data $h = go($sSql); echo $sSql . '<table cellpadding="1" cellspacing="0" border="1">'; while ($o = mysql_fetch_field($h)) $a[] = $o->name; echo '<tr><th>' . implode('</th><th>', $a) . '</th></tr>'; while ($r = mysql_fetch_row($h)) echo '<tr><td>' . implode('</td><td>', $r) . '</td></tr>'; echo '</table><br />'; } dbconn('localhost', 'test', 'root', '123'); go('DROP TABLE IF EXISTS account, account_to_group'); go('CREATE TABLE account (id int NOT NULL, title varchar(100) NOT NULL) ENGINE=MyISAM COLLATE=utf8_latvian_ci'); go('CREATE TABLE account_to_group (account_id int NOT NULL, group_id int NOT NULL) ENGINE=MyISAM COLLATE=utf8_latvian_ci'); go('INSERT account (id, title) VALUES (123,' . qs('1. akounts') . '),(456,' . qs('2. akounts') . '),(789,' . qs('3. akounts') . ')'); go('INSERT account_to_group (account_id, group_id) VALUES (123,1),(123,2),(123,3),(456,4),(456,5),(456,6),(789,7)'); esql('SELECT a.*, ag.group_id FROM account AS a LEFT JOIN account_to_group AS ag ON a.id = ag.account_id'); esql('SELECT a.*, ag.group_id FROM account AS a LEFT JOIN account_to_group AS ag ON a.id = ag.account_id WHERE a.id = 123'); Edited February 20, 2010 by 2easy Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted February 20, 2010 Author Report Share Posted February 20, 2010 (edited) Pakaļā, izrādās, ka kverija beigās biju iebāzis group by id. :D 2easy, vai tavā veidā tas ir ātrāk, drošāk un kā citādi labāk izmantot f-jas, lai taisītu savienojumu ar db kā šajā veidā (zemāk), ja viņu ieliek lapas augšā, bet tad pie katra mysql_query("......", $db); ? $db_username = ""; $database = ""; $db_password = ""; $hostname = ""; $db = mysql_connect($hostname,$db_username,$db_password); mysql_select_db($database,$db); Edited February 20, 2010 by thesnarkie Quote Link to comment Share on other sites More sharing options...
2easy Posted February 20, 2010 Report Share Posted February 20, 2010 (edited) connection vajag padot tikai tad, kad ir vairāki dažādi connection vienlaicīgi. ar drošību tam nav nekāda sakara. tā kā parasti 99.99% vienlaicīgi vajag tikai vienu datubāzi, tad funkcijai mysql_query() pietiek padot tikai sqlu Edited February 20, 2010 by 2easy Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted February 20, 2010 Author Report Share Posted February 20, 2010 esql('SELECT a.*, ag.group_id FROM account AS a LEFT JOIN account_to_group AS ag ON a.id = ag.account_id WHERE ag.group_id = 2') Ja es piemēram gribu parādīt tikai rindas, kur ag.group_id ir vienāds ar 2, bet nav vienāds ar 1 un 3. Kā to izdarīt? Es mēģināju dažādi: ag.group_id='2' AND ag.group_id!='1' AND ag.group_id!='3' vai ag.group_id='2' AND ag.group_id NOT IN ('1', '3') Bet abi nedarbojās pareizi. Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted February 21, 2010 Report Share Posted February 21, 2010 Ja iedotu tabulu struktūru, datus un vēlamo rezultātu, tad būtu krietni labāk saprotams, __kas__ nedarbojas pareizi un __kā__ tavuprāt ir pareizi. Tikmēr vari palasīt par savienojumiem vispār, varbūt palīdz ;) Gints Plivna http://datubazes.wordpress.com/ Quote Link to comment Share on other sites More sharing options...
2easy Posted February 21, 2010 Report Share Posted February 21, 2010 (edited) ag.group_id='2' AND ag.group_id!='1' AND ag.group_id!='3' es nemaz tālāk neatbildēju, jo paskatoties uz to kodu, nodomāju, ka viņš vnk jokojās. un uz joku būtu muļķīgi atbildēt ar nopietnu atbildi :P 1) ja kkas ir vienāds ar 2, tad nevajag taisīt kontrolšāvienus un pārbaudīt, vai tas nav vienāds ar 1 vai 3 vai vēl sazin ko 2) skaitļus nevajag likt apostrofos 3) droši vien atkal ir ielicis kko no sērijas "GROUP BY", kā rezultātā viņam nestrādā pat vnkārša salīdzināšana :D go('DROP TABLE IF EXISTS account, account_to_group'); go('CREATE TABLE account (id int NOT NULL, title varchar(100) NOT NULL) ENGINE=MyISAM COLLATE=utf8_latvian_ci'); go('CREATE TABLE account_to_group (account_id int NOT NULL, group_id int NOT NULL) ENGINE=MyISAM COLLATE=utf8_latvian_ci'); go('INSERT account (id, title) VALUES (123,' . qs('1. akounts') . '),(456,' . qs('2. akounts') . '),(789,' . qs('3. akounts') . ')'); go('INSERT account_to_group (account_id, group_id) VALUES (123,1),(123,2),(123,3),(456,1),(456,2),(456,4),(789,7)'); esql('SELECT a.*, ag.group_id FROM account AS a LEFT JOIN account_to_group AS ag ON a.id = ag.account_id'); esql('SELECT a.*, ag.group_id FROM account AS a LEFT JOIN account_to_group AS ag ON a.id = ag.account_id WHERE ag.group_id = 2'); Edited February 21, 2010 by 2easy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.