Swee Hong
Swee Hong

Reputation: 539

php mysql get 2 table value

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!

Upvotes: 0

Views: 52

Answers (4)

KTAnj
KTAnj

Reputation: 1356

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");

Upvotes: 0

Ramki
Ramki

Reputation: 519

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

you can use like this

Upvotes: 1

Gunni
Gunni

Reputation: 519

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;
}

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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

Upvotes: 0

Related Questions