开发者

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

开发者 https://www.devze.com 2023-03-31 20:31 出处:网络
My company let me to extract data from database according to different conditions, and present those data in a HTML table. And I also need to add a new field in the table to show corresponding categor

My company let me to extract data from database according to different conditions, and present those data in a HTML table. And I also need to add a new field in the table to show corresponding category code according to those different conditions. There are 11 different conditions need to be deal with. What is the best way to do it? Writing 11 queries and put them into 11 functions? or putting them into the same function and judging开发者_C百科 the returned value.

This the first independent assignment in my new company and is very important to me, so please help me~~

I am sorry I didn't make it clear. I am using PHP and MYSQL. Those 11 conditions are similar to each other. I know how to write the queries, but I don't know how to manipulate and sort the returned value, and how to put them into the same HTML table. I also need to add a new field in the table to differentiate their type.

I attached some code here:

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>


If you are a newbie like me then W3Schools has a simple example at the following link for PHP, MySQL, HTML http://www.w3schools.com/php/php_mysql_where.asp


SQL-wise, since you're always looking for the same columns, this looks like a UNION operation. If the criteria changes for each query are only the dates, and all the dates will be known up front, I'd consider writing one function that does the UNIONs for each query in the Sql, and returns your final table, rather than separating out the queries.

Here is an example of how I would write this in a stored procedure, and you could execute the stored procedure, passing in the parameters you want, and the result would be your final table. This may not exactly match MySQL's syntax for union, or for stored procs, but this should give you an idea.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号