2011-08-31 68 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

如果這有幫助,請考慮投票。 –