2011-08-31 95 views
0

我的公司让我根据不同的条件从数据库中提取数据,并将这些数据呈现在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> 
+0

请提供样本,并可能编辑您的问题,这不是很清楚。 –

+0

您使用什么Web编程语言来创建/填充HTML表格?这是什么类型的数据库? –

+0

您是否有查询提取需要添加字段的数据集?所有11个条件都基于单列中的值吗?我们需要更多信息来帮助您。 –

回答

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 
+0

非常感谢您的回复。你能给我一个如何联合每个查询结果的例子吗?我还需要在表格中添加一个新字段来区分它们的类型。我怎么做?谢谢 –

+0

如果这有帮助,请考虑投票。 –