Jump to content

Inner join...


Go to solution Solved by arunpatal,

Recommended Posts

$result = mysql_query("SELECT s.sub_cat_id, s.category_id, s.sub_cat_name, t.id, t.topic_title
        FROM subcategory s INNER JOIN datatb t ON(s.sub_cat_id=t.sub_id)
        ORDER BY s.sub_cat_id $DESC_ASC, t.id $DESC_ASC LIMIT 5");

 

 

The number of limit should be from subcategory table but its geting limit of datatb..... ????????

Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/
Share on other sites

lets say i hav 6 subcategory and each subcategory have 10 entrys from datatb table...

I want to limit subcategory to 5 but then problem is that its showing only one subcategory with 5 entrys.

 

I want to display 5 rows of subcategory

Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460428
Share on other sites

lets say i hav 6 subcategory and each subcategory have 10 entrys from datatb table...

I want to limit subcategory to 5 but then problem is that its showing only one subcategory with 5 entrys.

 

I want to display 5 rows of subcategory

 

Then, GROUP BY each subcategory entries which exist in datatb table, count only these which have for example minimum 10 records using a HAVING clause and finaly LIMIT them to 5. That's all. 

 

PS: My logic about this query here is something like that:

$result = mysql_query("
        SELECT s.sub_cat_id, s.category_id, s.sub_cat_name, t.id, t.topic_title
        FROM subcategory s INNER JOIN datatb t ON (s.sub_cat_id=t.sub_id)
        GROUP BY s.sub_cat_name
        HAVING(COUNT(*) >= 1)
        ORDER BY s.sub_cat_id $DESC_ASC, t.id $DESC_ASC LIMIT 5");
Edited by jazzman1
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460452
Share on other sites

I need 5 rows of subcategroy....

 

The code above brings 5 row of subcategorys but it shows only 1 item in each subcategory..

 

Like this

 

 

Subcat1.

 itrem1

 

subcat2

 item2

 

subcat3

 item3.....   and so on.....

 

I want it to be like this

 

subcat 1

 item1, item2, item3

subcat 2

 item1, item2

subcat 3

 item1, item2, itme3, item4

subcat 4

 item1, item2

subcat 5

 item1, item2, itme3, item4, item5

Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460466
Share on other sites

Try something like this (untested)

SELECT s.sub_cat_name
, GROUP_CONCAT(t.topic_title ORDER BY t.id $DESC_ASC SEPARATOR ', ')
FROM subcategory s 
INNER JOIN datatb t 
    ON(s.sub_cat_id=t.sub_id)
GROUP BY s.sub_cat_id
ORDER BY s.sub_cat_id $DESC_ASC,  
LIMIT 5
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460499
Share on other sites

 

Try something like this (untested)

SELECT s.sub_cat_name
, GROUP_CONCAT(t.topic_title ORDER BY t.id $DESC_ASC SEPARATOR ', ')
FROM subcategory s 
INNER JOIN datatb t 
    ON(s.sub_cat_id=t.sub_id)
GROUP BY s.sub_cat_id
ORDER BY s.sub_cat_id $DESC_ASC,  
LIMIT 5

 

 

It gives error like this

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\my_site\functions.php on line 28

Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460540
Share on other sites

At last! I've being trying to connect to the forum for over 3 hours.

 

Yes, I missed the comma when I was rearranging your query. When I run with my test data I get this

mysql> SELECT s.sub_cat_name
    -> , GROUP_CONCAT(t.topic_title ORDER BY t.id DESC SEPARATOR ', ') as items
    -> FROM subcategory s
    -> INNER JOIN datatb t
    ->     ON(s.sub_cat_id=t.sub_id)
    -> GROUP BY s.sub_cat_id
    -> ORDER BY s.sub_cat_id DESC
    -> LIMIT 5;
+--------------+--------------------+
| sub_cat_name | items              |
+--------------+--------------------+
| subcat f     | mmm, kkk           |
| subcat e     | lll, jjj           |
| subcat d     | iii, hhh, ggg, fff |
| subcat c     | eee, ddd           |
| subcat b     | ccc                |
+--------------+--------------------+

test data:

 

 

 

mysql> select * from subcategory;
+------------+--------------+
| sub_cat_id | sub_cat_name |
+------------+--------------+
|          1 | subcat a     |
|          2 | subcat b     |
|          3 | subcat c     |
|          4 | subcat d     |
|          5 | subcat e     |
|          6 | subcat f     |
+------------+--------------+

mysql> select * from datatb;
+----+--------+-------------+
| id | sub_id | topic_title |
+----+--------+-------------+
|  1 |      1 | aaa         |
|  2 |      1 | bbb         |
|  3 |      2 | ccc         |
|  4 |      3 | ddd         |
|  5 |      3 | eee         |
|  6 |      4 | fff         |
|  7 |      4 | ggg         |
|  8 |      4 | hhh         |
|  9 |      4 | iii         |
| 10 |      5 | jjj         |
| 11 |      5 | lll         |
| 12 |      6 | kkk         |
| 13 |      6 | mmm         |
+----+--------+-------------+

 

 

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460545
Share on other sites

I think it is because of loop.......

 

Check this code......

$categories = array();
    $result = mysql_query("SELECT *
    , GROUP_CONCAT(t.topic_title ORDER BY t.id DESC SEPARATOR ', ') as items
    FROM $subcategory s
    INNER JOIN $datatb t
    ON(s.sub_cat_id=t.sub_id)
    GROUP BY s.sub_cat_id
    ORDER BY s.sub_cat_id DESC
    LIMIT 5;");

    while($row = mysql_fetch_assoc($result)){
    $categories[$row['sub_cat_id']]['subcat'] = array('name' => $row['sub_cat_name']);
    $categories[$row['sub_cat_id']]['topics'][$row['id']] = array('name' => $row['topic_title'], 'id' => $row['id']);
    }

    foreach($categories as $sub_cat_id => $row):
    
    echo "<div id='subcat_list'>".strtoupper($row['subcat']['name'])."</div>";
        
        foreach($row['topics'] as $sub_id => $sub_row):
         
         echo "<div class='topic_list' id='topic_list$sub_row[id]'> <a href='review.php?topic_id=$sub_row[id]&datatb=$datatb'>";
        echo strtoupper($sub_row['name']);
        echo "</a></div>";
    
        endforeach;
        
    echo  "<br>";
    endforeach;
    }};
Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460547
Share on other sites



$sql = "SELECT s.sub_cat_name
, GROUP_CONCAT(t.topic_title ORDER BY t.id $DESC_ASC SEPARATOR ', ') as items
FROM subcategory s
INNER JOIN datatb t
ON(s.sub_cat_id=t.sub_id)
GROUP BY s.sub_cat_id
ORDER BY s.sub_cat_id $DESC_ASC
LIMIT 5";
$res = $db->query($sql);
while ($row = $res->fetch_assoc()) {
echo "<strong>{$row['sub_cat_name']}</strong><br>{$row['items']}<br><br>";
}

/** results ****************
subcat a
aaa, bbb

subcat b
ccc

subcat c
ddd, eee

subcat d
fff, ggg, hhh, iii

subcat e
jjj, lll

****************************/
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460549
Share on other sites

This is how my topic table look like......

Table name PHP

 

id int auto_increment primary key,
    topic_title mediumtext,
    topic_detail mediumtext,
    added_date date,
    sub_id int(11) This is sub_cat_id from subcategory table

 

 

And this is my subcategory table look like

 

sub_cat_id int auto_increment primary key,
sub_cat_name varchar(500),
category_id int(11)

Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460550
Share on other sites

Ok.........

Its working

"SELECT s.sub_cat_name, s.sub_cat_id, t.topic_title, t.id
    , GROUP_CONCAT(t.topic_title ORDER BY t.id DESC SEPARATOR ', ') as topic_title
    FROM $subcategory s
    INNER JOIN $datatb t
    ON(s.sub_cat_id=t.sub_id)
    GROUP BY s.sub_cat_id
    ORDER BY s.sub_cat_id DESC
    LIMIT 5;"

But new problem is that all topics are showing as one link........

 

Something like this  Item1,Item2,Item3

 

i am sure it have to do with loop....

 

This is the new code to echo ..

while($row = mysql_fetch_assoc($result)){
    $categories[$row['sub_cat_id']]['subcat'] = array('name' => $row['sub_cat_name']);
    $categories[$row['sub_cat_id']]['topics'][$row['id']] = array('name' => $row['topic_title'], 'id' => $row['id']);
    }

    foreach($categories as $sub_cat_id => $row):
    
    echo "<div id='subcat_list'>".strtoupper($row['subcat']['name'])."</div>";
        
        foreach($row['topics'] as $sub_id => $sub_row):
         
         echo "<div class='topic_list' id='topic_list$sub_row[id]'> <a href='review.php?topic_id=$sub_row[id]&datatb=$datatb'>";
        echo strtoupper($sub_row['name']);
        echo "</a></div>";
    
        endforeach;
        
    echo  "<br>";
    endforeach;
    }};
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460554
Share on other sites

Can you show us the output of next foreach:

foreach($categories as $sub_cat_id => $row):
    
    echo "<div id='subcat_list'>".strtoupper($row['subcat']['name'])."</div>";
        
        foreach($row['topics'] as $sub_id => $sub_row):
         
        // here 
         echo "<pre>".print_r($sub_row,true)."</pre>"; 
    
        endforeach;
        
    echo  "<br>";
    endforeach;
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460557
Share on other sites

I want it to be like this

 

subcat 1

 item1, item2, item3

subcat 2

 item1, item2

subcat 3

 item1, item2, itme3, item4

subcat 4

 item1, item2

subcat 5

 item1, item2, itme3, item4, item5

 

... which is exactly what the query and my code gives you. Now you introduce the need for links. When you get people's time for free, don't tell them you want one thing, then, when they have done it, tell them you wanted something different. It wastes their time. Good luck.

Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460558
Share on other sites

 

Can you show us the output of next foreach:

foreach($categories as $sub_cat_id => $row):
    
    echo "<div id='subcat_list'>".strtoupper($row['subcat']['name'])."</div>";
        
        foreach($row['topics'] as $sub_id => $sub_row):
         
        // here 
         echo "<pre>".print_r($sub_row,true)."</pre>"; 
    
        endforeach;
        
    echo  "<br>";
    endforeach;

 

 

This is the result

Subcategory 1
Array
(
    [name] => topic1, topic2, topic3, topic4
    [id] => 6
)

Subcategory 2
Array
(
    [name] => topic1, topic2, topic3, topic4
    [id] => 8
)
Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460559
Share on other sites

... which is exactly what the query and my code gives you. Now you introduce the need for links. When you get people's time for free, don't tell them you want one thing, then, when they have done it, tell them you wanted something different. It wastes their time. Good luck.

 

Sorry about that........  I could not explain nicely at the beginning

Link to comment
https://forums.phpfreaks.com/topic/284343-inner-join/#findComment-1460560
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.