[英]How to handle SQL query returned value like this?(PHP&MySQL)
My company let me to extract data from database according to different conditions, and present those data in a HTML table. 我的公司让我根据不同的条件从数据库中提取数据,并将这些数据显示在HTML表中。 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.
需要处理11种不同的条件。 What is the best way to do it?
最好的方法是什么? Writing 11 queries and put them into 11 functions?
编写11个查询并将它们放入11个函数中? or putting them into the same function and judging 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.
我正在使用PHP和MYSQL。 Those 11 conditions are similar to each other.
这11个条件彼此相似。 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.
我知道如何编写查询,但不知道如何操作和排序返回的值,以及如何将它们放入同一HTML表中。 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>
如果您是像我这样的新手,那么W3Schools在下面的链接中提供了一个简单的示例,用于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. 在SQL方面,由于您一直在寻找相同的列,因此这看起来像UNION操作。 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.
如果每个查询的条件更改仅是日期,并且所有日期都将预先知道,我将考虑编写一个函数,该函数对Sql中的每个查询执行UNION,并返回最终表,而不是分离出来查询。
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.
这可能与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.