Click here to
get yummy grain feed
delivered to your RSS oven

rss

  • Businesswing Design BLOG
  • Assist to join the global market.
14-3 變更資料SQL語法

Date:五月 24, 2011 | Author:bwingnet

前面所提到的是查詢資料表內資料的語法,其實SQL語法內也有一些語法是可變更資料的語法。變更資料的語法包涵了新增(insert into)、刪除(delete)與更新(update)。

14-3-1 備份資料

由於新增(insert into)、刪除(delete)與更新(update)均會變更資料表內容,為了避免影響原有資料,在此我們使用SQL語法備份原資料表為新資料表,備份指令為:「create table 新資料表 select * from 舊資料表」,這個章節內我們需要將多個資料表進行備份,所以請您於phpMyAdmin的SQL視窗內依序輸入以下語法後按下執行鈕執行:

【表3、備份原有資料表】
語法
create table insertemployees select * from employees;
create table insertjob select * from job;
create table updateemployees1 select * from employees;
create table updateemployees2 select * from employees;
create table updateemployees3 select * from employees;
create table updatejob select * from job;
create table updateproducts select * from products;
create table delemployees1 select * from employees;
create table delemployees2 select * from employees;
create table delemployees3 select * from employees;
create table deljob select * from job;

新增(insert into)、刪除(delete)與更新(update)的練習都將以剛剛所建立的資料表為練習對象。

+Read more

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

  Categories: 網頁程式PHP,網頁設計 | Tags: ,
        
14-2 討論與日期有關的查詢

Date:五月 23, 2011 | Author:bwingnet

介紹完多個資料表之間的資料查詢後,接著說明與日期有關的函數,在此先整理如以下列表(在此僅列出日期部分):

【表1、用於SQL語法的MySQL日期函數】
分類 函數名稱 作用
年份 year(date) 返回date的年份,範圍在1000到9999。
四季 quarter(date) 返回date屬於一年中第幾季,範圍1到4。
月份 month(date) 返回date的月份。
  monthname(date) 返回date的月份名稱。
星期 dayofweek(date) 返回日期date的星期索引(1=星期天,2=星期一, ……7=星期六)。
  dayname(date) 返回date的星期名稱。
日期 dayofmonth(date) 返回date的月份中日期,在1到31範圍內。
  dayofyear(date) 返回date在一年中的日數, 在1到366範圍內。
日期間隔 datediff(date1,date2) 返回兩個日期之間天數。
  date_add(date,interval expr type) 日期添加指定的時間間隔
+Read more

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

  Categories: 網頁程式PHP,網頁設計 | Tags: ,
        
第十四章 多個資料表查詢與變更資料

Date:五月 18, 2011 | Author:bwingnet

上一章我們已經學習如何在一個資料表內進行資料查詢,但如果資料放在兩個資料表內,且同時要做搜尋,那該怎麼辦呢?另外,想要新增、刪除、更新資料,又該如何呢?本章將進一步說明如何於兩個資料表內進行查詢及進行新增、刪除與更新。

在進行各種SQL動作查詢之前,請您開啟「phpMyAdmin」,檢查「pcschool」資料庫內是否有employees、job、duty這幾個資料表,這幾個資料表將於本章用到,請務必點選光碟內「13」資料夾「SQL」資料夾,匯入這幾個資料表。

14-1 多個資料表查詢

您可以使用「inner join」的語法將兩個資料表內相同的資料列出,例如您想查詢「employees」資料表及「job」資料表的「employeeid」相同下的「employees.firstname, employees.city, job.title」這幾個欄位內容,而兩個資料表內都有九筆資料。您可於在「phpMyAdmin」內挑選「pcschool」資料庫後點選右上方的「SQL」,輸入「select employees.firstname, employees.city, job.title from employees inner join job on employees.employeeid = job.employeeid;」後執行。

php_ch14-01【圖1、兩個資料表進行inner join查詢】

+Read more

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

  Categories: 網頁程式PHP,網頁設計 | Tags: ,
         top