繁体   English   中英

如何处理这样的SQL查询返回值?(PHP&MySQL)

[英]How to handle SQL query returned value like this?(PHP&MySQL)

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

如果您是像我这样的新手,那么W3Schools在下面的链接中提供了一个简单的示例,用于PHP,MySQL和HTML http://www.w3schools.com/php/php_mysql_where.asp

在SQL方面,由于您一直在寻找相同的列,因此这看起来像UNION操作。 如果每个查询的条件更改仅是日期,并且所有日期都将预先知道,我将考虑编写一个函数,该函数对Sql中的每个查询执行UNION,并返回最终表,而不是分离出来查询。

这是我如何在存储过程中编写此示例的示例,您可以执行该存储过程,并传入所需的参数,结果将是最终表。 这可能与MySQL的联合或存储proc语法不完全匹配,但这应该可以给您一个思路。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM