我正在做一個(gè)有兩個(gè)表的項(xiàng)目,其中一個(gè)是用戶表,另一個(gè)是部門表。在這里,我已經(jīng)將部門表的外鍵連接到用戶表并檢查它是否正常工作。(獲取特定外鍵的數(shù)據(jù))。現(xiàn)在我的要求是在一個(gè)表中我想顯示所有用戶以及外鍵值。這里。require_once "database.php";$result = "SELECT * FROM Users";// , Department WHERE Department.Department_ID = Users.Dpt_id$output = mysqli_query($conn, $result);<?php while ($single = $output->fetch_assoc()):?> <tr> <td><?php echo $single['firstname']; ?></td> <td><?php echo $single['lastname']; ?></td> <td><?php echo $single['email']; ?></td> <td><?php echo $single['phnumber']; ?></td> <td><?php echo $single['provider']; ?></td> <td><?php echo $single['location']; ?></td> <td><?php echo $single['Dpt_id']; ?> </td> <td><?php if ($single['Dadmin'] == 1){ echo '<p>Department admin</p>'; } elseif($single['Superuser'] == 1){ echo '<p>SUPER</p>'; } else{ echo '<p>USER</p>'; } ?></td><?php endwhile ?>試過的代碼:$result = "SELECT * FROM Users, Department WHERE Department.Department_ID = Users.Dpt_id";$output = mysqli_query($conn, $result);所以這是我的代碼。請(qǐng)幫我從外鍵中獲取值。
2 回答

BIG陽
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
我想你想要這個(gè)結(jié)果,但我不知道你的列名:
SELECT u.*, d.department_name
FROM USERS u
LEFT JOIN Department d
ON u.dpt_id = d.department_id

皈依舞
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
試試這個(gè)查詢:
SELECT u.*, d.department_id
FROM USERS u
INNER JOIN Department d
ON u.dpt_id = d.department_id
- 2 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報(bào)
0/150
提交
取消