# SQL - how to sort by id's from two databases!

**URL:** <https://forum.kirupa.com/t/sql-how-to-sort-by-ids-from-two-databases/236517>\
**Category:** programming\
**Created:** [August 21, 2007, 5:28pm UTC](https://forum.kirupa.com/t/sql-how-to-sort-by-ids-from-two-databases/236517 "2007-08-21T17:28:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patch](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/patch/32/1095_2.png) [@Patch](https://forum.kirupa.com/u/Patch)\
**Post date:** [August 21, 2007, 5:28pm UTC](https://forum.kirupa.com/t/sql-how-to-sort-by-ids-from-two-databases/236517/1 "2007-08-21T17:28:20Z")

</div>

Hi guys,

Need a little help, I’m working on a blog and I need help with something. I’m still learning PHP and mySQL, so please forgive my ignorance lol.

Basically I have two database tables one which contains all the articles, date, title and so on and the other contains all the comments.

I have it so that I can comment on articles, this works fine. But on my index I want to display how many comments have been made in each article. I have it so on the article page it displays how many comments have been made and it’s probably something similar.

Here is the code I have for my recordsets etc: -

```php

mysql_select_db($database_cr_blog, $cr_blog);
$query_entry = "SELECT * FROM cr_entry ORDER BY id DESC";
$query_limit_entry = sprintf("%s LIMIT %d, %d", $query_entry, $startRow_entry, $maxRows_entry);
$entry = mysql_query($query_limit_entry, $cr_blog) or die(mysql_error());
$row_entry = mysql_fetch_assoc($entry);

if (isset($_GET['totalRows_entry'])) {
  $totalRows_entry = $_GET['totalRows_entry'];
} else {
  $all_entry = mysql_query($query_entry, $cr_blog) or die(mysql_error());
  $totalRows_entry = mysql_num_rows($all_entry);
}
$totalPages_entry = ceil($totalRows_entry/$maxRows_entry)-1;

$colname_comments = "-1";
if (isset($_GET['id'])) {
  $colname_comments = $_GET['id'];
}
mysql_select_db($database_cr_blog, $cr_blog);
$query_comments = sprintf("SELECT * FROM cr_entry_comments WHERE entry_id", GetSQLValueString($colname_comments, "int"));
$comments = mysql_query($query_comments, $cr_blog) or die(mysql_error());
$row_comments = mysql_fetch_assoc($comments);
$totalRows_comments = mysql_num_rows($comments);

```

I think that’s it, sorry I’m using dreamweaver for most of this, I’m trying to ween myself off it! lol.

I’m thinking that I have to make it so that the entry\_id in the comments matches the id in cr\_entry.

Something like this: -

```php

"SELECT * FROM cr_entry_comments WHERE entry_id = FROM cr_entry WHERE id";

```

Now, I know that’s not it, but if someone could help me figure it out I will be very grateful,

Thanks in advanced!
