我的公司让我根据不同的条件从数据库中提取数据,并将这些数据呈现在HTML表格中。我还需要在表格中添加一个新字段,以根据不同的条件显示相应的类别代码。有11种不同的条件需要处理。最好的方法是什么?编写11个查询并将它们分成11个函数?或者将它们放入相同的函数并判断返回的值。
这是我新公司的第一份独立任务,对我来说非常重要,所以请帮助我~~
对不起,我没说清楚。我正在使用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>
答案 0 :(得分:0)
如果你是像我这样的新手,那么W3Schools在PHP,MySQL,HTML的以下链接中有一个简单的例子http://www.w3schools.com/php/php_mysql_where.asp
答案 1 :(得分:0)
SQL方面,因为您总是在寻找相同的列,所以这看起来像是UNION操作。如果每个查询的条件更改只是日期,并且所有日期都是预先知道的,我会考虑编写一个函数为Sql中的每个查询执行UNION,并返回最终表,而不是分离出来查询。
这是一个如何在存储过程中编写它的示例,您可以执行存储过程,传入所需的参数,结果将是您的最终表。 这可能与MySQL的union或者存储过程的语法不完全匹配,但这应该给你一个想法。
CREATE PROCEDURE usp_getMailableUsers(
@ClientID INT --(I'm assuming they are integers)
@HireDateRangeStart --(put whatever datatype your hire dates are stored in here)
@HireDateRangeEnd --(put whatever datatype your hire dates are stored in here)
@HraDateRangeStart -- (put whatever datatype your hire dates are stored in here)
@HraDateRangeEnd -- (put whatever datatype your hire dates are stored in here)
@ScreenDateRangeStart -- (put whatever datatype your hire dates are stored in here)
@ScreenDateRangeEnd -- (put whatever datatype your hire dates are stored in here)
-- add any other parameters needed here.
)
AS
BEGIN
-- Query 1 goes here
(
SELECT
users.id,
clients.name AS client,
users.social_security_number AS ssn,
users.hiredate
FROM users u
INNER JOIN clients c
ON u.client_id = c.id
INNER JOIN hra h
ON u.id = h.user_id
INNER JOIN screening s
ON u.id = s.user_id
WHERE
u.client_id = @ClientID
AND (u.hiredate BETWEEN @HireDateRangeStart
AND @HireDateRangeEnd
OR u.hiredate IS NULL)
AND h.date BETWEEN @HraDateRangeStart AND @HraDateRangeEnd
AND h.maileddate IS NULL
AND (s.date < @ScreenDateRangeEnd
OR s.Date > @ScreenDateRangeStart)
GROUP BY u.id, c.name, u.social_security_number, u.hiredate
)
UNION
-- Query 2 goes here
(
SELECT
users.id,
clients.name AS client,
users.social_security_number AS ssn,
users.hiredate
FROM users u
INNER JOIN clients c
ON u.client_id = c.id
INNER JOIN hra h
ON u.id = h.user_id
LEFT JOIN screening s
ON u.id = s.user_id
WHERE
u.client_id = @ClientID
AND (u.hiredate BETWEEN @HireDateRangeStart AND @HireDateRangeEnd OR u.hiredate IS NULL)
AND h.date BETWEEN @HraDateRangeStart AND @HraDateRangeEnd
AND h.maileddate IS NULL
AND (s.date < @ScreenDateRangeEnd OR s.Date > @ScreenDateRangeStart)
GROUP BY u.id, c.name, u.social_security_number, u.hiredate
)
UNION
-- Query 3 goes here
(
-- put code for query 3 in this set, etc.
)
-- Repeat the word UNION and further queries until you are at the last one.
END