To find a string in a certain field and replace it with another string:

update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

Credit http://www.mediacollege.com/computer/database/mysql/find-replace.html

ตัวอย่างตาราง

id name type price
123451 Park’s Great Hits Music 19.99
123452 Silly Puddy Toy 3.99
123453 Playstation Toy 89.95
123454 Men’s T-Shirt Clothing 32.50
123455 Blouse Clothing 34.97
123456 Electronica 2002 Music 3.99
123457 Country Tunes Music 21.55
123458 Watermelon Food 8

MySQL SUM – Totaling Groups


SUM
is an aggregate function that totals a specific column for a group. The “products” table that is displayed above has several products of various types. One use of SUM might be to find the total of all the items’ price for each product type.

Just as we did in the aggregate introduction lesson, we are going to apply the aggregate function to price and GROUP BY type to create four groups: Music, Toy, Clothing and Food.

PHP and MySQL Code:

<?php
// Make a MySQL Connection

$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['type']. " = $". $row['SUM(price)'];
echo "<br >";
}
?>

Display:

Total Clothing = $67.47
Total Food = $8.73
Total Music = $45.53
Total Toy = $93.94

โค้ดโปรแกรม
<?php

####### รูปแบบของวันที่ ที่อาจจะเก็บลงในฐานข้อมูลแบบนี้ ######
$start_date=”17/03/2007″; // วันที่เริ่มใช้บริการ
$expire_date=”17/03/2008″;//วันสิ้นสุดการใช้บริการ
$today_date=date(“d/m/Y “);//วันที่ของวันนี้

## ฟังก์ชั่น explode ใช้แยกเครื่องหมาย ( / ) ออกจากข้อความ แล้วเอาข้อความที่ได้ไปแยกเก็บลงใน array ##
/* วันที่เริ่มใช้บริการ */
$start_explode = explode(“/”, $start_date);
$start_day = $start_explode[0];
$start_month = $start_explode[1];
$start_year = $start_explode[2];

/*วันสิ้นสุดการใช้บริการ*/
$expire_explode = explode(“/”, $expire_date);
$expire_day = $expire_explode[0];
$expire_month = $expire_explode[1];
$expire_year = $expire_explode[2];

/*วันที่ของวันนี้*/
$today_explode = explode(“/”, $today_date);
$today_day = $today_explode[0];
$today_month = $today_explode[1];
$today_year = $today_explode[2];

/* ฟังก์ชั่น gregoriantojd เป็นฟังก์ชั่นที่เอาไว้แปลงวันที่ เพื่อใช้คำนวณหาจำนวนวัน
รูปแบบการใช้งาน gregoriantojd ( int $month, int $day, int $year )
โฮสที่ใช้ต้องทำการเปิด extension calendar ของ php ด้วย ถึงจะใช้งานได้*/

$start = gregoriantojd($start_month,$start_day,$start_year);
$expire = gregoriantojd($expire_month,$expire_day,$expire_year);
$today = gregoriantojd($today_month,$today_day,$today_year);

$period_of_time  = $expire-$start; //หาระยะเวลาการใช้งาน
$date_current=$expire-$today;//หาวันที่เหลืออยู่

echo “เริ่มใช้บริการวันที่ :”,$start_date,”<br>”;
echo “สิ้นสุดวันใช้บริการวันที่ :”,$expire_date,”<br>”;
echo “ระยะเวลาการใช้งาน : “,$period_of_time ,” วัน<br>” ;

echo “<b>วันนี้</b> ….วันที่ $today_date ขณะนี้จึงเหลือเวลาอีก :”,$date_current,” วัน จึงจะถึงวันที่ $expire_date  ซึ่งเป็นวันหมดอายุการใช้งาน<br>”;

?>

ผลลัพธ์จะออกมาเช่นนี้

เริ่มใช้บริการวันที่ :17/03/2007
สิ้นสุดวันใช้บริการวันที่ :17/03/2008
ระยะเวลาการใช้งาน : 366 วัน
วันนี้ ….วันที่ 18/03/2007 ขณะนี้จึงเหลือเวลาอีก :365 วัน จึงจะถึงวันที่ 17/03/2008 ซึ่งเป็นวันหมดอายุการใช้งาน

ลองเอาไปประยุกต์ใส่เงื่อนไขกันดูครับ
เช่น เช็คค่าถ้าเหลือ 15 วัน ส่งเมลล์แจ้งเตือนอัติโนมัติให้ลูกค้ามาต่ออายุ  อะไรทำนองนี้อ่ะครับ

วิธีการนำโค้ดข้างต้นไปใช้สำหรับโฮสที่ไม่ได้ลง extension calendar ของ php
พอดีผมทำให้ลูกค้าเค้าจะเอาไปใช้ที่โฮสเค้า
พอไปลงโปรแกรมปรากฎว่าโปรแกรมรันไม่ได้ครับ
เพราะว่าโฮสที่เค้าเช่าไม่ได้ลง extension calendar ของ php
ก็เลยไป Search หาวิธีมา เจอว่า  ถ้าไม่ได้ลง ก็ไม่เป็นไร
ไม่ต้องไปบอกให้เจ้าของ Server เค้าลงให้ หรือว่าเปิดให้ใช้งาน

เพียงเอาฟังก์ชั่นด้านล่างนี้ใส่ แล้ว include ไปใช้งาน  ก็ใช้งานได้ล่ะครับ
<?php

function GregorianToJD ($month,$day,$year) {

if ($month > 2) {

$month = $month – 3;

} else {

$month = $month + 9;
$year = $year – 1;

}

$c = floor($year / 100);
$ya = $year – (100 * $c);
$j = floor((146097 * $c) / 4);
$j += floor((1461 * $ya)/4);
$j += floor(((153 * $month) + 2) / 5);
$j += $day + 1721119;
return $j;

}
?>

ลองดูครับ

โอเคล่ะครับ เมื่อกี้กดไวไปนิด
ไปล่ะ… ไปเที่ยวตลาดคลองถมดีก่า วันนี้มีตอนกลางคืน

ลิงค์ที่เกี่ยวข้อง
http://th2.php.net/calendar
http://th2.php.net/GregorianToJD
http://ez.no/community/contribs/datatypes/birthday_datatype

http://www.thaihosttalk.com/th1/index.php?topic=5832.0

Quote

http://www.zend.com/en/community/pdt
http://www-128.ibm.com/developerworks/ibm/…&ca=dkw-php

สมัยก่อนใช้แต่ editplus ตอนนี้มี debugger ใช้แล้วเท่ห์จิงๆ

แหล่งที่มา http://www.witcomram.com/webboard/index.php?showtopic=6260
โดยคุณ dragon_html [at] witcomram.com

http://www.thaihosttalk.com/th1/index.php?topic=10333.0

Products Table:

id name type price
123451 Park’s Great Hits Music 19.99
123452 Silly Puddy Toy 3.99
123453 Playstation Toy 89.95
123454 Men’s T-Shirt Clothing 32.50
123455 Blouse Clothing 34.97
123456 Electronica 2002 Music 3.99
123457 Country Tunes Music 21.55
123458 Watermelon Food 8.73

PHP and MySQL Code:

<?php
// Make a MySQL Connection
 $query = “SELECT type, COUNT(name) FROM products GROUP BY type”;
 $result = mysql_query($query) or die(mysql_error());
// Print out result while($row = mysql_fetch_array($result))
{ echo “There are “. $row['COUNT(name)'] .” “. $row['type'] .” items.”;  
echo “<br />”;  }
 ?>

Display:

There are 2 Clothing items.
There are 1 Food items.
There are 3 Music items.
There are 2 Toy items.