Jump to content

How can I do this in pure MySQL?


Azu

Recommended Posts

Hello

 

Right now I only know how to do this in PHP, and it only recurses as many times as I put the code.

 

What is the MySQL query that will do this without PHP, and without a limit on recursions? Please help.. I'm stumped..

 

set_time_limit(120);

$a0=timed_query("select `id` from `db`.`Topics`");

while($b0=mysqli_fetch_row($a0)){$q=0;$x=0;

$x=mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b0[0]"));

if(!$a1=timed_query("select `id` from `db`.`Topics` where `parent`=$b0[0]"))break;

$q=mysqli_num_rows($a1);

while($b1=mysqli_fetch_row($a1)){

if(!$a2=timed_query("select `id` from `db`.`Topics` where `parent`=$b1[0]"))break;

$q=$q+mysqli_num_rows($a2);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b1[0]"));

while($b2=mysqli_fetch_row($a2)){

if(!$a3=timed_query("select `id` from `db`.`Topics` where `parent`=$b2[0]"))break;

$q=$q+mysqli_num_rows($a3);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b2[0]"));

while($b3=mysqli_fetch_row($a3)){

if(!$a4=timed_query("select `id` from `db`.`Topics` where `parent`=$b3[0]"))break;

$q=$q+mysqli_num_rows($a4);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b3[0]"));

while($b4=mysqli_fetch_row($a4)){

if(!$a5=timed_query("select `id` from `db`.`Topics` where `parent`=$b4[0]"))break;

$q=$q+mysqli_num_rows($a5);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b4[0]"));

while($b5=mysqli_fetch_row($a5)){

if(!$a6=timed_query("select `id` from `db`.`Topics` where `parent`=$b5[0]"))break;

$q=$q+mysqli_num_rows($a6);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b5[0]"));

while($b5=mysqli_fetch_row($a5)){

if(!$a6=timed_query("select `id` from `db`.`Topics` where `parent`=$b5[0]"))break;

$q=$q+mysqli_num_rows($a6);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b5[0]"));

while($b5=mysqli_fetch_row($a5)){

if(!$a6=timed_query("select `id` from `db`.`Topics` where `parent`=$b5[0]"))break;

$q=$q+mysqli_num_rows($a6);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b5[0]"));

while($b5=mysqli_fetch_row($a5)){

if(!$a6=timed_query("select `id` from `db`.`Topics` where `parent`=$b5[0]"))break;

$q=$q+mysqli_num_rows($a6);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b5[0]"));

while($b5=mysqli_fetch_row($a5)){

if(!$a6=timed_query("select `id` from `db`.`Topics` where `parent`=$b5[0]"))break;

$q=$q+mysqli_num_rows($a6);

$x=$x+mysqli_result(timed_query("select count(*) from `db`.`Posts` where `question_id`=$b5[0]"));}}}}}}}}}

timed_query("update `db`.`Topics` set `subs`=$q,`reply`=$x where `id`=$b0[0]");}}

exit;}

Link to comment
Share on other sites

Topics is like this topic, and posts is like, well, this post. And you can make topics inside of other topics, unlimited, as deep as you want.

 

The code above fixes the counts for the amount of replies in a topic, and amount of sub-topics in it, since my forum isn't finished/perfect yet those values get messed up sometimes.

Link to comment
Share on other sites

So noone wants to help? (I mean besides saying "OMIGAWD LRN2USE JOINZ LUUUL" which isn't helpful at all)

 

We're just looking for some effort on your end to work towards a solution....

 

select t.id count(p.question_id) from topics as t left join posts as p on p.question_id=t.id group by t.id

 

Will give you the # of posts per topic.  If you want to deal with subcategories, you'll need to join them in as I said earlier.

Link to comment
Share on other sites

We're just looking for some effort on your end to work towards a solution....
Sorry it's just that I have no clue how to do this and in 12 replies the only info I got was "learn how to use joins" and I have no clue how to make use of that.

 

select t.id count(p.question_id) from topics as t left join posts as p on p.question_id=t.id group by t.id

 

Will give you the # of posts per topic.  If you want to deal with subcategories, you'll need to join them in as I said earlier.

Thank you :) will try that.
Link to comment
Share on other sites

  • 1 month later...

Well.. thanks for trying to help me ^^ I give up though. I've been trying for a long time and I still can't get this to work. And when I try to use the query you posted it gives an error

Error Code : 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'count(p.question_id) from topics as t left join posts as p on p.question_id=t.id' at line 1

 

 

 

=/

 

I guess I'll try using postgresql or something and see if I have better luck in that? Since it seems this can't be done in MySQL...

Link to comment
Share on other sites

The one I quoted from you near the top of the page.

 

And I didn't mean that MySQL is bad because of the one error sorry.

 

I just mean that I really want to get this working and I've been trying a long time and can't figure out how to do it in MySQL so I'm starting to think it can't be done in MySQL x_x;

Link to comment
Share on other sites

Oh okay thanks ^^ I was putting it in the wrong place. It stopped giving an error now. But it seems to only return the post count 1 level deep..

 

I could have done that with just a simple select id,(select count(*) from posts where question_id=topics.id) from topics

 

It needs to recurse as deep as it goes. That's what I don't know how to do in MySQL..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.