我的公司讓我根據不同的條件從數據庫中提取數據,並將這些數據呈現在HTML表格中。而且我還需要在表格中添加一個新字段,以根據這些不同的條件顯示相應的類別代碼。有11個不同的條件需要處理。什麼是最好的方式來做到這一點?寫11個查詢並將它們放入11個函數中?或者將它們放入相同的函數並判斷返回的值。如何處理這樣的SQL查詢返回值?(PHP&MySQL)
這在我的新公司第一個獨立分配,並且是對我很重要,所以請大家幫幫我~~
我很抱歉,我沒有說清楚。我正在使用PHP和MYSQL。這11個條件彼此相似。我知道如何編寫查詢,但我不知道如何操作和排序返回的值,以及如何將它們放入同一個HTML表格中。我還需要在表格中添加一個新字段來區分它們的類型。
我連着在這裏的一些代碼:
function getMailableUserlist(){
global $_db;
$mailableQuery1 = "
SELECT users.id, clients.name AS client, users.social_security_number AS ssn, users.hiredate FROM users
INNER JOIN clients
ON(
users.client_id = clients.id
)
INNER JOIN hra
ON(
users.id = hra.user_id
)
INNER JOIN screening
ON(
users.id = screening.user_id
)
WHERE users.client_id = '1879'
AND (users.hiredate BETWEEN '2011-01-01' AND '2011-08-14'
OR users.hiredate IS NULL)
AND hra.date BETWEEN '2011-07-01' AND '2011-11-15'
AND hra.maileddate IS NULL
AND screening.date BETWEEN '2011-05-15' AND '2011-11-15'
AND screening.maileddate IS NULL
GROUP BY users.id";
$mailableQuery2 = "
SELECT users.id, clients.name AS client, users.social_security_number AS ssn, users.hiredate, hra.date AS hra, screening.date AS screening FROM users
INNER JOIN clients
ON(
users.client_id = clients.id
)
INNER JOIN hra
ON(
users.id = hra.user_id
AND hra.date + INTERVAL 30 DAY >= NOW()
)
LEFT JOIN screening
ON(
users.id = screening.user_id
)
WHERE users.client_id = '1879'
AND (users.hiredate BETWEEN '2011-01-01' AND '2011-08-14'
OR users.hiredate IS NULL)
AND hra.date BETWEEN '2011-07-01' AND '2011-11-15'
AND hra.maileddate IS NULL
AND (screening.date < 2011-05-15 OR screening.date > 2011-11-15)
GROUP BY users.id";
There are 9 more queries coming...............
$result = $_db->getResultsForQuery($mailableQuery1);
return $result;
The table are as follows:
<table id="unmailedScreeningstable" class="reportTable">
<thead>
<tr>
<th>User ID</th>
<th>client</th>
<th>ssn</th>
<th>hiredate</th>
</tr>
</thead>
<tbody>
<?php foreach(getProvenaMailableUserlist() as $userlist) { ?>
<tr user_id="<?php echo $userlist['id']; ?>">
<td><?php echo $userlist['id']; ?></td>
<td><?php echo $userlist['client']; ?></td>
<td><?php echo $userlist['ssn']; ?></td>
<td><?php echo $userlist['hiredate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
請提供樣本,並可能編輯您的問題,這不是很清楚。 –
您使用什麼Web編程語言來創建/填充HTML表格?這是什麼類型的數據庫? –
您是否有查詢提取需要添加字段的數據集?所有11個條件都基於單列中的值嗎?我們需要更多信息來幫助您。 –