在我的数据库中,我有3个表。
用户(表名) 如此多的行是用户名之一,如果
--------------------
| id | user |
--------------------
| 7 | user |
| 8 | user_name_2 |
| 11 | user_name_5 |
--------------------
和另一个表格调用数据
----------------------------
| id | user-id | number |
----------------------------
| 1 | 7 | 789654125 |
| 2 | 8 | 465654545 |
| 3 | 11 | 884554511 |
----------------------------
在表td中,user_id是表用户ID
现在我想在php中显示姓名和号码
$conn = mysqli_connect("localhost","root","QAZWS12","user");
$sql = "SELECT * FROM user"
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
}
相同并获得$number = $row["number"]
现在我要在数据表用户ID中自动获取表用户号,我该怎么做?
最终输出
用户789654125 像这样的user_name_2 465654545
答案 0 :(得分:2)
You have to use join here. For more details on different types JOINS refer either official MySQL doc or search online
replace this query
$sql = "SELECT u.id,u.user AS user_name,d.number AS user_number
FROM user u LEFT JOIN description d ON u.id = d.user-id";
The above query will fetch user id and username and user number
replace this too
$id = $row["id"].' '.$row['user_name'].' '.$row['user_number'].'<br/>';
This outputs as 7 user 789654125
答案 1 :(得分:1)
Try below SQL query with LEFT JOIN to get the username with their mobile number in the result set:
$sql = "SELECT u.user, d.number
FROM user as u
LEFT JOIN data as d ON d.user-id = u.id
"
答案 2 :(得分:1)
用户SQL左联接
$sql = "SELECT * FROM user LEFT JOIN call_data ON user.id = call_data.user-id";
访问数据时
$number = $row['number'];