如何使用带有外键的 SQL INNER JOIN 和 ALIAS 从数据库中获取数据?
How do I use SQL INNER JOIN & ALIAS with foreign keys to fetch data from a database?
我正在尝试在 table 中显示数据,如下面的代码所示。我正在尝试从名为 users:
的数据库 table 中获取 Requisitions table(view) 中这 3 列的数据
- 征用者
- 批准人
- 支付方
数据库中的表如下。我将省略似乎正常工作的列。
fin_requisitions
| req_id | req_by | approved_by | disbursed_by |
| ------ | ------ | ----------- | ------------ |
| 1 | 1 | 2 | 3 |
| 2 | 1 | 2 | 3 |
用户 table
| id | username |
| ------ | ------ |
| 1 | Lisa |
| 2 | Anne |
| 3 | John |
输出始终是 Lisa,即使它应该是不同的用户名。我是一个菜鸟,但根据我到目前为止所学的知识,可以通过正确使用 SQL ALIAS 轻松解决问题,但我不知道如何解决,所以我问,我该如何解决这个问题?
<table class="table table-striped">
<thead>
<tr>
<td>ID</td>
<td>File</td>
<td>Expense Category</td>
<td>Amount Requisitioned</td>
<td>Details</td>
<td>Requisitioned By</td>
<td>Requisition Date</td>
<td>Approval Status</td>
<td>Amount Approved</td>
<td>Approved By</td>
<td>Date Approved</td>
<td>Disbursement Status</td>
<td>Amount Disbursed</td>
<td>Disbursed By</td>
<td>Date Disbursed</td>
<td>Note</td>
<td colspan=2>Actions</td>
</tr>
</thead>
<tbody>
<hr>
<?php
$sql = "
SELECT fin_requisition.req_id
, ops_files.file_name
, fin_expense_cats.expense_cat
, fin_requisition.amount
, fin_requisition.details
, users.username
, fin_requisition.req_date
, fin_approval_status.status
, fin_requisition.amt_approved
, users2.username
, fin_requisition.approval_date
, fin_disb_status.status
, fin_requisition.amt_disbursed
, users3.username
, fin_requisition.date_disbursed
, fin_requisition.notes FROM fin_requisition
JOIN ops_files
ON fin_requisition.file = ops_files.file_id
JOIN fin_expense_cats
ON fin_requisition.expense_cat = fin_expense_cats.cat_id
JOIN users
ON fin_requisition.req_by = users.id
JOIN fin_approval_status
ON fin_requisition.approval_status = fin_approval_status.status_id
JOIN users users2
ON fin_requisition.approved_by = users2.id
JOIN fin_disb_status
ON fin_requisition.disb_status = fin_disb_status.status_id
JOIN users users3
ON fin_requisition.disbursed_by = users3.id
ORDER
BY req_id ASC
";
$result = $mysqli->query($sql);
?>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo $row["req_id"]; ?></td>
<td><?php echo $row["file_name"]; ?></td>
<td><?php echo $row["expense_cat"]; ?></td>
<td><?php echo $row["amount"]; ?></td>
<td><?php echo $row["details"]; ?></td>
<td><?php echo $row["username"]; ?></td>
<td><?php echo $row["req_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_approved"]; ?></td>
<td><?php echo $row["username2"]; ?></td>
<td><?php echo $row["approval_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_disbursed"]; ?></td>
<td><?php echo $row["username3"]; ?></td>
<td><?php echo $row["date_disbursed"]; ?></td>
<td><?php echo $row["notes"]; ?></td>
<td>
<a href="edit_requisition.php?req_id=<?php echo ($row["req_id"]); ?>" class="btn btn-primary">Edit</a>
</td>
<td>
<a href="delete_requisition.php?req_id=<?php echo ($row["req_id"]); ?>" method="post">
<button class="btn btn-danger" type="submit" onclick="return confirm('Are you sure you want to delete this record?')">Delete</button>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
在 sql 中,您应该为不同的用户名使用正确的别名,例如:username usename2 , username3
SELECT fin_requisition.req_id
, ops_files.file_name
, fin_expense_cats.expense_cat
, fin_requisition.amount
, fin_requisition.details
, users.username
, fin_requisition.req_date
, fin_approval_status.status
, fin_requisition.amt_approved
, users2.username username2
, fin_requisition.approval_date
, fin_disb_status.status
, fin_requisition.amt_disbursed
, users3.username username3
, fin_requisition.date_disbursed
, fin_requisition.notes FROM fin_requisition
JOIN ops_files
ON fin_requisition.file = ops_files.file_id
JOIN fin_expense_cats
ON fin_requisition.expense_cat = fin_expense_cats.cat_id
JOIN users
ON fin_requisition.req_by = users.id
JOIN fin_approval_status
ON fin_requisition.approval_status = fin_approval_status.status_id
JOIN users users2
ON fin_requisition.approved_by = users2.id
JOIN fin_disb_status
ON fin_requisition.disb_status = fin_disb_status.status_id
JOIN users users3
ON fin_requisition.disbursed_by = users3.id
ORDER
BY req_id ASC
然后在php代码中引用这些列别名
<td><?php echo $row["req_id"]; ?></td>
<td><?php echo $row["file_name"]; ?></td>
<td><?php echo $row["expense_cat"]; ?></td>
<td><?php echo $row["amount"]; ?></td>
<td><?php echo $row["details"]; ?></td>
<td><?php echo $row["username"]; ?></td>
<td><?php echo $row["req_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_approved"]; ?></td>
<td><?php echo $row["username2"]; ?></td>
<td><?php echo $row["approval_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_disbursed"]; ?></td>
<td><?php echo $row["username3"]; ?></td>
<td><?php echo $row["date_disbursed"]; ?></td>
<td><?php echo $row["notes"]; ?></td>
这里有很多问题。
让我们从更简单的事情开始...
考虑以下几点:
DROP TABLE IF EXISTS requisitions;
CREATE TABLE requisitions
(req_id SERIAL PRIMARY KEY
,req_by INT NOT NULL
,approved_by INT NOT NULL
,disbursed_by INT NOT NULL
);
INSERT INTO requisitions VALUES
(1,2,4,3),
(2,4,4,2);
DROP TABLE IF EXISTS users ;
CREATE TABLE users
(id SERIAL PRIMARY KEY
,username VARCHAR(12) UNIQUE
);
INSERT INTO users VALUES
(1,'Lisa'),
(2,'Anne'),
(3,'John');
SELECT x.req_id
, r.username AS requisitioner -- the AS keyword is optional
, a.username AS approver -- and often
, d.username AS disburser -- omitted
FROM requisitions x
LEFT
JOIN users r
ON r.id = x.req_by
LEFT
JOIN users a
ON a.id = x.approved_by
LEFT
JOIN users d
ON d.id = x.disbursed_by;
+--------+---------------+----------+-----------+
| req_id | requisitioner | approver | disburser |
+--------+---------------+----------+-----------+
| 1 | Anne | NULL | John |
| 2 | NULL | NULL | Anne |
+--------+---------------+----------+-----------+
所以,没有丽莎。
看来,对于您提供的示例数据,您需要将 JOIN 更改为 lEFT JOIN。请看下面
WITH REQ(req_id,req_by,approved_by,disbursed_by) AS
(
SELECT 1,2,4,3
UNION ALL
SELECT 2,4,4,2
),
USERS(ID,USERNAME) AS
(
SELECT 1 , 'Lisa'
UNION ALL
SELECT 2,'Anne'
UNION ALL
SELECT 3,'JOHN'
)
SELECT R.REQ_ID,R.req_by,COALESCE(R_BY.USERNAME,'') AS REQUESTER,
R.approved_by,COALESCE(APPRR_BY.USERNAME,'')AS APPROVER,
R.disbursed_by,COALESCE(DISB_BY.USERNAME,'')AS DISBURSER
FROM REQ AS R
LEFT JOIN USERS AS R_BY ON R.req_by=R_BY.ID
LEFT JOIN USERS AS APPRR_BY ON R.req_by=APPRR_BY.ID
LEFT JOIN USERS AS DISB_BY ON R.disbursed_by=DISB_BY.ID
我正在尝试在 table 中显示数据,如下面的代码所示。我正在尝试从名为 users:
的数据库 table 中获取 Requisitions table(view) 中这 3 列的数据- 征用者
- 批准人
- 支付方
数据库中的表如下。我将省略似乎正常工作的列。
fin_requisitions
| req_id | req_by | approved_by | disbursed_by |
| ------ | ------ | ----------- | ------------ |
| 1 | 1 | 2 | 3 |
| 2 | 1 | 2 | 3 |
用户 table
| id | username |
| ------ | ------ |
| 1 | Lisa |
| 2 | Anne |
| 3 | John |
输出始终是 Lisa,即使它应该是不同的用户名。我是一个菜鸟,但根据我到目前为止所学的知识,可以通过正确使用 SQL ALIAS 轻松解决问题,但我不知道如何解决,所以我问,我该如何解决这个问题?
<table class="table table-striped">
<thead>
<tr>
<td>ID</td>
<td>File</td>
<td>Expense Category</td>
<td>Amount Requisitioned</td>
<td>Details</td>
<td>Requisitioned By</td>
<td>Requisition Date</td>
<td>Approval Status</td>
<td>Amount Approved</td>
<td>Approved By</td>
<td>Date Approved</td>
<td>Disbursement Status</td>
<td>Amount Disbursed</td>
<td>Disbursed By</td>
<td>Date Disbursed</td>
<td>Note</td>
<td colspan=2>Actions</td>
</tr>
</thead>
<tbody>
<hr>
<?php
$sql = "
SELECT fin_requisition.req_id
, ops_files.file_name
, fin_expense_cats.expense_cat
, fin_requisition.amount
, fin_requisition.details
, users.username
, fin_requisition.req_date
, fin_approval_status.status
, fin_requisition.amt_approved
, users2.username
, fin_requisition.approval_date
, fin_disb_status.status
, fin_requisition.amt_disbursed
, users3.username
, fin_requisition.date_disbursed
, fin_requisition.notes FROM fin_requisition
JOIN ops_files
ON fin_requisition.file = ops_files.file_id
JOIN fin_expense_cats
ON fin_requisition.expense_cat = fin_expense_cats.cat_id
JOIN users
ON fin_requisition.req_by = users.id
JOIN fin_approval_status
ON fin_requisition.approval_status = fin_approval_status.status_id
JOIN users users2
ON fin_requisition.approved_by = users2.id
JOIN fin_disb_status
ON fin_requisition.disb_status = fin_disb_status.status_id
JOIN users users3
ON fin_requisition.disbursed_by = users3.id
ORDER
BY req_id ASC
";
$result = $mysqli->query($sql);
?>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo $row["req_id"]; ?></td>
<td><?php echo $row["file_name"]; ?></td>
<td><?php echo $row["expense_cat"]; ?></td>
<td><?php echo $row["amount"]; ?></td>
<td><?php echo $row["details"]; ?></td>
<td><?php echo $row["username"]; ?></td>
<td><?php echo $row["req_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_approved"]; ?></td>
<td><?php echo $row["username2"]; ?></td>
<td><?php echo $row["approval_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_disbursed"]; ?></td>
<td><?php echo $row["username3"]; ?></td>
<td><?php echo $row["date_disbursed"]; ?></td>
<td><?php echo $row["notes"]; ?></td>
<td>
<a href="edit_requisition.php?req_id=<?php echo ($row["req_id"]); ?>" class="btn btn-primary">Edit</a>
</td>
<td>
<a href="delete_requisition.php?req_id=<?php echo ($row["req_id"]); ?>" method="post">
<button class="btn btn-danger" type="submit" onclick="return confirm('Are you sure you want to delete this record?')">Delete</button>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
在 sql 中,您应该为不同的用户名使用正确的别名,例如:username usename2 , username3
SELECT fin_requisition.req_id
, ops_files.file_name
, fin_expense_cats.expense_cat
, fin_requisition.amount
, fin_requisition.details
, users.username
, fin_requisition.req_date
, fin_approval_status.status
, fin_requisition.amt_approved
, users2.username username2
, fin_requisition.approval_date
, fin_disb_status.status
, fin_requisition.amt_disbursed
, users3.username username3
, fin_requisition.date_disbursed
, fin_requisition.notes FROM fin_requisition
JOIN ops_files
ON fin_requisition.file = ops_files.file_id
JOIN fin_expense_cats
ON fin_requisition.expense_cat = fin_expense_cats.cat_id
JOIN users
ON fin_requisition.req_by = users.id
JOIN fin_approval_status
ON fin_requisition.approval_status = fin_approval_status.status_id
JOIN users users2
ON fin_requisition.approved_by = users2.id
JOIN fin_disb_status
ON fin_requisition.disb_status = fin_disb_status.status_id
JOIN users users3
ON fin_requisition.disbursed_by = users3.id
ORDER
BY req_id ASC
然后在php代码中引用这些列别名
<td><?php echo $row["req_id"]; ?></td>
<td><?php echo $row["file_name"]; ?></td>
<td><?php echo $row["expense_cat"]; ?></td>
<td><?php echo $row["amount"]; ?></td>
<td><?php echo $row["details"]; ?></td>
<td><?php echo $row["username"]; ?></td>
<td><?php echo $row["req_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_approved"]; ?></td>
<td><?php echo $row["username2"]; ?></td>
<td><?php echo $row["approval_date"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><?php echo $row["amt_disbursed"]; ?></td>
<td><?php echo $row["username3"]; ?></td>
<td><?php echo $row["date_disbursed"]; ?></td>
<td><?php echo $row["notes"]; ?></td>
这里有很多问题。
让我们从更简单的事情开始...
考虑以下几点:
DROP TABLE IF EXISTS requisitions;
CREATE TABLE requisitions
(req_id SERIAL PRIMARY KEY
,req_by INT NOT NULL
,approved_by INT NOT NULL
,disbursed_by INT NOT NULL
);
INSERT INTO requisitions VALUES
(1,2,4,3),
(2,4,4,2);
DROP TABLE IF EXISTS users ;
CREATE TABLE users
(id SERIAL PRIMARY KEY
,username VARCHAR(12) UNIQUE
);
INSERT INTO users VALUES
(1,'Lisa'),
(2,'Anne'),
(3,'John');
SELECT x.req_id
, r.username AS requisitioner -- the AS keyword is optional
, a.username AS approver -- and often
, d.username AS disburser -- omitted
FROM requisitions x
LEFT
JOIN users r
ON r.id = x.req_by
LEFT
JOIN users a
ON a.id = x.approved_by
LEFT
JOIN users d
ON d.id = x.disbursed_by;
+--------+---------------+----------+-----------+
| req_id | requisitioner | approver | disburser |
+--------+---------------+----------+-----------+
| 1 | Anne | NULL | John |
| 2 | NULL | NULL | Anne |
+--------+---------------+----------+-----------+
所以,没有丽莎。
看来,对于您提供的示例数据,您需要将 JOIN 更改为 lEFT JOIN。请看下面
WITH REQ(req_id,req_by,approved_by,disbursed_by) AS
(
SELECT 1,2,4,3
UNION ALL
SELECT 2,4,4,2
),
USERS(ID,USERNAME) AS
(
SELECT 1 , 'Lisa'
UNION ALL
SELECT 2,'Anne'
UNION ALL
SELECT 3,'JOHN'
)
SELECT R.REQ_ID,R.req_by,COALESCE(R_BY.USERNAME,'') AS REQUESTER,
R.approved_by,COALESCE(APPRR_BY.USERNAME,'')AS APPROVER,
R.disbursed_by,COALESCE(DISB_BY.USERNAME,'')AS DISBURSER
FROM REQ AS R
LEFT JOIN USERS AS R_BY ON R.req_by=R_BY.ID
LEFT JOIN USERS AS APPRR_BY ON R.req_by=APPRR_BY.ID
LEFT JOIN USERS AS DISB_BY ON R.disbursed_by=DISB_BY.ID