0
我需要根據單個表中選定的日期顯示所有員工的出勤率。在php中執行mysql查詢以獲取列和行
這是我的MySQL查詢:
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN ca.Date = ''',
date_format(Date, '%Y-%m-%d'),
''' THEN coalesce(p.Out_time) END) AS `',
date_format(Date, '%Y-%m-%d'), '`'
)
) INTO @sql
FROM attendance
where Date>='2015-04-27'
and Date <= '2015-04-30';
SET @sql
= CONCAT('SELECT ca.User_name,
ca.User_id,
', @sql, '
from
(
select c.Date, a.User_name, a.User_id
from attendance c
cross join attendance a
) ca
left join attendance p
on ca.User_id = p.User_id
and ca.Date = p.Date
where ca.Date>=''2015-04-27''
and ca.Date <= ''2015-04-30''
group by ca.User_name, ca.User_id
order by ca.User_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
它是在MySQL(它顯示的結果)執行罰款,但同時在PHP它不顯示任何內容執行,
$qqq=mysql_query($sss);
while($rrr=mysql_fetch_array($qqq))
{
echo $rrr;
}
它扔這些警告:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\Xampp\htdocs\php\temp.php on line 118
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\Xampp\htdocs\php\temp.php on line 119
我的表結構是這樣的:
CREATE TABLE `attendance` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`User_id` varchar(50) NOT NULL,
`User_name` varchar(100) NOT NULL,
`Date` date DEFAULT NULL,
`In_time` time DEFAULT NULL,
`Out_time` time DEFAULT NULL,
`Time_diff` decimal(5,2) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `Id_UNIQUE` (`Id`)
)
發佈您的php代碼不起作用 – Dalvik
$ sss =「MySQL query above above」; $ qqq = mysql_query($ sss); ($ rrr = mysql_fetch_array($ qqq)) { echo $ rrr; } – user3530708
請發佈$ sss的聲明,這是問題出現的地方。 – Chad