Jump to content
php.lv forumi

MySQL apakšvaicājumi


Jackal

Recommended Posts

SELECT id, title, b.topics FROM forum_category AS a, (SELECT COUNT(id) AS topics, category AS category FROM forum_topic WHERE category = a.id) AS b

 

Parādās kļūda: Unknown column 'a.id' in 'where clause'

 

Kāpēc apakšvaicājumā nevar izmantot datus no citas tabulas, jeb es ko esmu līki uzrakstijis?

Link to comment
Share on other sites

Šāds variants neder, tagad tiek izvadīts tikai viens ieraksts no forum_category tabulas.

Ir divas tabulas forum_category (id, title), form_topic (id, category_id, title).

Mana doma bija ar vienu vaicājumu izvilkt visus forum_category tabulas ierakstus, katrai kategorijai saskaitīt topikus no forum_topic tabulas.

Edited by Jackal
Link to comment
Share on other sites

Nop. forum_category tabulā ir divi ieraksti un forum_topic tabulā arī ir divi ieraksti (katrai kategorijai pa vienam).

Tavs dotais piemērs izvada tikai vienu kategoriju un saskaita visus forum_topic tabulas ierakstus neatkarīgi no tā kurai kategorijai ieraksts piesaistīts.

Link to comment
Share on other sites

Negribējās taisīt jaunu tēmu, tāpēc jautašu tepat. Tagad mans vaicajums izskatas šadi:

 

SELECT a.id, a.title, a.description, COUNT(b.id) AS topics, b.title AS topic, COUNT(c.id) AS replays, c.date AS date, c.user AS user 
FROM forum_category AS a                                  
LEFT JOIN (forum_topic AS b)                 
ON (a.id = b.category AND b.status = '1')                                   
LEFT JOIN (forum_replay AS c)                
ON (b.id = c.topic AND b.status = '1')                                  
GROUP BY b.category

 

Kļūdas neparāda, bet ir vajadzība no forum_topic un forum_replay izvilkt ierakstu ar lielāko datumu. Mēģināju darīt šādi:

 

SELECT a.id, a.title, a.description, COUNT(b.id) AS topics, b.title AS topic, COUNT(c.id) AS replays, c.date AS date, c.user AS user 
FROM forum_category AS a                                  
LEFT JOIN (forum_topic AS b)                 
ON (a.id = b.category AND b.status = '1' ORDER BY b.date DESC)                                   
LEFT JOIN (forum_replay AS c)                
ON (b.id = c.topic AND b.status = '1' ORDER BY c.date DESC)                                  
GROUP BY b.category

 

bet tad parādās sintakses kļūda

Edited by Jackal
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...