0

Now I would like to query data to do the newest get code.

Now I have 2 table,

this is t1.

+--------------+--------------------------+----------------------+
|    id        |  name                    | description          |
+--------------+--------------------------+----------------------+
| 1            | GG                       | GG is good           |
| 2            | ABC DEFG                 | ABC DDDDD            |
| 3            | CCARD                    | Gooooo               |
+--------------+--------------------------+----------------------+

this is t2

+---------+------------+-------------------+------------------+
| id      | kaid       | code              | timestamp        |
+---------+------------+-------------------+------------------+
| 1       | 2          | ZZZZAAAAA         | 123456789        |
| 2       | 2          | AAAZZADWWW        | 123344444        |
| 3       | 1          | ASFASDFFFF        | 123333333        |
| 4       | 2          | HHHHHFDFG         | 123222222        |
| 5       | 1          | ASDASDADDDD       | 123111111        |
+---------+------------+-------------------+------------------+

I want the data show like this:

ORDER BY timestamp desc limit 5

+--------+------------+------------------+------------------
| id     | kaid       | name             | time             |
+--------+------------+------------------+------------------+
| 1      | 1          | GG               | 123111111        |
| 2      | 2          | ABC DEFG         | 123222222        |
| 3      | 1          | GG               | 123333333        |
| 4      | 2          | ABC DEFG         | 123344444        |
| 5      | 2          | ABC DEFG         | 123456789        |
+--------+------------+------------------+------------------+

now my code is:

$querylist = mysql_query("SELECT * FROM t1 ORDER BY time desc limit 5");
while($rowlist = mysql_fetch_row($querylist)) {
    $idlist[] = $rowlist['id'];
    $user_list_latest[] = $rowlist;
}

how do I select the t2.name when t1.id = t2.kaid?

thank you very much!

Swee Hong
  • 539
  • 2
  • 12
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 24 '15 at 11:56

4 Answers4

1
SELECT t1.*,t2.name FROM t1,t2 where t1.id = t2.kaid  ORDER BY t1.time desc limit 5

you can use like this

Ramki
  • 519
  • 2
  • 9
0

You need to use join like as

select t1.name, t2.* from t2 
join t1 on t2.kaid = t1.id
order by time desc limit 5
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0

You have to join the tables like this, and like Jay said stop using mysql_* functions:

$querylist = mysql_query("SELECT t1.*, t2.name FROM t1 INNER JOIN t2 ON t1.id=t2.kaid ORDER BY t1.time desc limit 5");
while($rowlist = mysql_fetch_row($querylist)) {
    $idlist[] = $rowlist['id'];
    $user_list_latest[] = $rowlist;
}
Gunni
  • 519
  • 5
  • 14
0

Try this

$querylist = mysql_query("SELECT t2.*, t1.name FROM t2
                          INNER JOIN t1
                          ON t1.id=t2.kaid
                          ORDER BY t2.time desc 
                          LIMIT 5");
Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29
KTAnj
  • 1,346
  • 14
  • 36