两个顺序选择表的最佳方式

时间:2017-12-23 09:40:41

标签: php mysql

您好我有两个表来发送连续查询。

例如,表A产生1,2,3 .. 然后,在查询1,2,3 ..

的表B中查找数据

TABLEA

_____________________
|   uid |   rate    |
|   1   |   1       |
|   1   |   2       |
|   1   |   3       |
|   2   |   4       |

tableB的

_________________________
|   rate    |   text    |
|   1       |   ONE     |
|   2       |   TWO     |
|   3       |   THREE   |
|   4       |   FOUR    |

===

<?php
    $sql = $con->query("SELECT * FROM tableA WHERE uid=1");
    $user = $sql->fetch_array();
    $ratings = $user['rate']; //1,2,3

    $sql2 = $con->query("SELECT * FROM tableB WHERE rate IN('".$ratings."')");
    $text = $sql2->fetch_array();
    $results = $text['text']; //ONE, TWO, THREE
?>

如何做到最好?

1 个答案:

答案 0 :(得分:0)

您可以使用此查询:

Select tableA.*,tableB.* 
from tableA 
join tableB on tableA.rate=tableB.rate 
where tableA.uid=1