"SELECT nuber_new, SUM(tot_up_price) AS tot_up_price FROM lottery
where (select_new = 'เลข3ตัว') and (tot_up_price > 0)
GROUP by nuber_new HAVING SUM(tot_up_price) > $chk1
ORDER BY tot_up_price DESC"
statement ที่ จขกทเขียน ผมงง ชื่อ field ครับ เลยได้แต่เดา
ผิดพลาดยังไงก็ขออภัย
Code (PHP)
$strSQL = <<<SQL
SELECT nuber_new, SUM(tot_up_price) AS ttl
FROM (
select * , group_concat( a ) as ga from (
select * from (
SELECT *, LEFT(select_new,1) as a FROM `lottery` union all
SELECT *, SUBSTRING(select_new,2, 1) as a FROM `lottery` union all
SELECT *, RIGHT(select_new,1) FROM `lottery`
) as t order by a
) as t1 group by no_data # ดู no_data ให้ดีนะครับ ผมว่าไม่ได้เอา ฟีลด์จริง มาให้ผม แค่สร้าง ตัวอย่างมาใช่ไหมครับ
) as t2 GROUP BY by ga
HAVING ttl > $chk1
ORDER BY ttl DESC
SQL;
ตอนนี้สร้างตารางมาแบบครบแล้ว ลอง run ดู เหมือน ซิมแทคมันผิด ยัง งงกับการใช้ ซิมแทคของของท่าน Chaidhanan
ไม่รู้ว่าใส่แบบนี้ถูกหรือปล่าว
Code (PHP)
<?
$chk1=$_POST[textfield];
$chk2=$_POST[textfield2];
$chk1=$chk1+0;
$chk2=$chk2+0;
include "../connect.php";
$strSQL = "SELECT nuber_new, SUM(tot_up_price) AS tot_up_price
FROM ( select * , group_concat( a ) as ga from (
select * from (
SELECT *, LEFT(select_new,1) as a FROM `lottery` union all
SELECT *, SUBSTRING(select_new,2, 1) as a FROM `lottery` union all
SELECT *, RIGHT(select_new,1) FROM `lottery`
) as t order by a
) as t1 group by no_data
) as t2 GROUP BY by ga
HAVING tot_up_price > $chk1
ORDER BY tot_up_price DESC"
;
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="350" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" align="center" valign="middle" bgcolor="#FFFFFF"><strong>โต๊ด
<input type="submit" name="Submit" value="พิมพ์" onclick="javascript:this.style.display='none';window.print()" />
</strong></td>
</tr>
<tr>
<td width="50" align="center" valign="middle" bgcolor="#CCFFFF">ลำดับ</td>
<td width="51" align="center" valign="middle" bgcolor="#CCFFFF">เลข</td>
<td width="117" align="center" valign="middle" bgcolor="#CCFFFF">ราคา</td>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td align="center" valign="middle"><?php echo ++$i; ?></td>
<td align="center" valign="middle"><?
$a=array();
$ar=array();
//while( $ro=$rs->fetch_assoc()){
for($i=0; $i<3; $i++) $a[]=substr($objResult['nuber_new'],$i,1);
$ar[]=implode('', $a);
//}
print_r(array_unique( $ar));
?></td>
<td align="right" valign="middle"><?=$objResult["tot_up_price"]-$chk2;?>
</td>
</tr>
<?
}
?>
สร้าง function ไว้ใน database ใช้ phpmyadmin run คำสั่งข้างล่างครับ จะได้ function sort_num_3 มาใช้
ไว้จัดเรียงตัวเลข 3 ตัว
Code (SQL)
DELIMITER $$
CREATE FUNCTION sort_num_3 (num char(3))
RETURNS char(3) DETERMINISTIC
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_num varchar(3) DEFAULT "";
DECLARE rs_num varchar(3) DEFAULT "";
DEClARE num_cursor CURSOR FOR
SELECT A FROM (
SELECT LEFT(num,1) A union all
SELECT SUBSTRING(num,2,1) union all
SELECT RIGHT(num,1)
) T ORDER BY A;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN num_cursor;
get_num: LOOP
FETCH num_cursor INTO v_num;
IF v_finished = 1 THEN
LEAVE get_num;
END IF;
SET rs_num = CONCAT(rs_num, v_num );
END LOOP get_num;
CLOSE num_cursor;
RETURN rs_num;
END$$
DELIMITER ;
Code (PHP)
$strSQL = "SELECT ga, SUM(tot_up_price) AS tot_up_price
FROM (
select tot_up_price , sort_num_3( nuber_new) as ga
FROM lottery
) as t
GROUP BY ga
HAVING tot_up_price > $chk1
ORDER BY SUM(tot_up_price) DESC"