mysql php-mysql shell-mysql

來源:互聯網
上載者:User

標籤:mysql php-mysql shell-mysql

net stop mysql


net start mysql


mySQL -uroot -p  --tee=c:\log.log


show databases;


create database dbname;


drop database dbname;


show databases;


use dbname


show tables


frm表結構,myd表資料,myi表索引



create table user(id int,name varchar(30),password varchar(30));


desc user;


select * from user


insert into user(id,name,password) values(1,"jack","pwd");



delete from user where id=2


update user set name=‘jay‘ where id=1


rename table tbname1 to tbname2


alter table tbname add pass varchar(32) not null default "password"    // 添加欄位


alter table tbname modify age int not null default 20;


alter table tbname change name newname varchar(30);


alter table tbname drop age;


drop table tbname if exists tbname



----------------------------------------------------------------------------設計

1.數值 int

create table tname(id int);


desc tname

//type int(11) 有符號


create table tname(id int unsigned zerofill);


desc tname

//type int(10) 無符號


//int(3)與長度無關,不夠3位時,前面補0,預設不顯示


//auto_increment

//not null

//default 

//primary key



2.字串

char(n) //最大255

varchar(n)

text  //65535位元組

longtext //42億位元組


3.時間

盡量使用時間戳整型儲存



奇怪的null值



\s  //查看mysql的基本資料


//查看資料庫字元編碼

show create database dbname


//查看錶字元集

show create table tbname


//

set names utf8


desc select * from user where name="jack"\G     //查看運行參數


//主鍵索引 

primary key


//普通索引

index in_named


? show //?類似於linux的man


show  index from t2; //查看錶中所有索引


//添加普通索引

alter  table tbname add index in_name(name)



//刪除普通索引

alter table t2 drop index in_named




ddl //資料定義語言 (Data Definition Language) create drop alter

dml  //資料操作語言  insert update  delete

dql  //資料查詢語言 select

dcl   //資料控制語言 grant commit rollback




-------------------------------------------------------------

insert into tbname(name) values("jack");

 

update tbname set name="wood" where id=1


delete from tbname where id=2;


delete from tbname where id in(1,3.5);

delete from tbname where id=1 or id=3 or id=5


delete from tbname where id>=3 and id<=5;

delete from tbname where id between 3 and 5;



WeiPHP、CakeWX 、 LaneWeChat,we7


-----------------------------------------------------------------

insert into table 插入多條資料


方法1:

insert into `ttt`

select ‘001‘,‘語文‘ union all

select ‘002‘,‘數學‘ union all

select ‘003‘,‘英語‘;


方法2:

INSERT INTO tab_comp VALUES(item1, price1, qty1),

(item2, price2, qty2),

(item3, price3, qty3);


方法3:

 INSERT INTO tab_comp(item1, price1, qty1) SELECT item1, price1, qty1 FROM tab_cc;


------------------------------------------------------------------

查詢

別名查詢

select username name from user

select username as name from user


distinct

select distinct age from user


//

空值null 注意 is null  / is not null 不可以用=

select * from user where addr is null


//like  注意%

//regexp

select * from user where name like ‘%s%‘;


select * from user where name regexp ‘.*4.*‘;



select * from user where name like ‘%4%‘ or name like ‘%5%‘;


select * from user where name regexp ‘(.*4.*)|(.*5.*)‘;


//order by asc(升序 預設)/desc(降序)

select * from user order by id

select * from user order by id asc


select * from user order by id desc


//limit

select * from user order by id desc limit 1;//取1個

select * from user order by id desc limit 0,3;//從第0個起取3個


//concat() 串連符

select concat("a","b");//輸出ab

select id,name,pass,concat(id,‘-‘,name) from user;


//rand()

select * from user order by rand() limit 3;//隨機取出3個


//count()

//sum()

//avg()

//max()

//min()

select count(id)  num from user;//統計id的個數,num別名

select count(*) num from user;//統計個數,注意:官方推薦使用


//

select sum(id) num from user;// 求和

select avg(id) num from user;//



//group by  having  order by  分組不能用where,只能用having

select name ,count(id) from mess group by name; //

select shopId ,count(id) num from `p_purchase_order_his` group by shopId order by num desc;

select name, count(id) tot from mess group by name having tot>=80 order by tot desc; //group by必須在order by前面


//after

alter table tbname add store int after id;


//多表查詢

普通查詢-多表

select * from tb1 tb2 ;//tb1的整個記錄加上tb2的一條條記錄

select * from tb1 tb2 where tb1.id=tb2.oid



左串連(把需要完全顯示的放到左邊) left join on

select * from tb1 left join tb2 on tb1.id=tb2.oid


右串連(把需要完全顯示的放到右邊) right join on



子查詢(巢狀查詢):一般情況下不使用

select name from user where id in(select uid from mess);


-------------------------------------------------

php mysql


header("content-type:text/html;charset=utf-8");


[email protected]_connect("localhost","root","root");

if(!conn){

echo  mysql_errno().<br/>.mysql_error();

}



mysql_select_db("test");


mysql_query("set names utf8");


mysql>create table t1(id int unsigned auto_increment primary key,username varchar(32),password varchar(32));



//insert

$username="allen";

$password="pwd";

$sql="insert into t1(username,password) values(‘{$username}‘,‘{$password}‘)";//注意單引號

//$sql="insert into t1(username,password) values(‘$username‘,‘$password‘)";//注意單引號

//echo $sql;

//mysql_query($sql);

if(mysql_query($sql)){

echo mysql_insert_id();

}



//update

$sql="update t1 set username=‘user1‘ where id=2";

mysql_query($sql);


//delete

$sql="delete from t1 where id=2";

mysql_query($sql);



//select  結果集

$sql="select username,addr from t1 order by id";

$res=mysql_query($res);// mysql resource

//mysql_fetch_assoc  關聯陣列

//mysql_fetch_row    索引數組

//mysql_fetch_array   混合數組

//mysql_fetch_object  對象


$row=mysql_fetch_assoc($res);


$row=mysql_fetch_row($res);


$row=mysql_fetch_array($res);


$row=mysql_fetch_object($res);


//

while($row=mysql_fetch_assoc($res)){

echo "<pre>";

print_r($row);

echo  "</pre>";

}

------------------------------------------

//取列資訊

while($f=mysql_fetch_field($res)){

echo "<pre>";

print_r($f->name);

echo  "</pre>";

}


//取列資訊

$sql="desc t1";

$res=mysql_query($sql);

while($row=mysql_fetch_assoc($res)){

echo "<pre>";

print_r($row[‘Field‘]);

echo "</pre>";

}


-----------------------------------------------------


mysql_insert_id()//取得上一步insert操作產生的id


mysql_affected_rows();//取得增、刪、改操作所影響的行數


mysql_num_rows()//取得查詢結果集中行的數目


select count(*) from tbname;//最快擷取表的行數


mysql_close($conn);



---------------------------------------------------------------------------------------------

shell mysql


chmod a+x xxx.sh  #給xxx.sh執行許可權

shell指令碼

----

#! /bin/bash

#mysql.sh


mysql="/usr/local/mysql/bin/mysql -uroot -p123"


#sql="show databases"

#sql="create table test.user(id int unsigned,name varchar(32),password varchar(32))"

#sql="desc test.user"

#sql="insert into test.user(username,password) values(‘jack‘,‘pwd‘)"


$mysql -e "$sql"


-----

-----

#! /bin/bash

#mysql.sh


conn="/usr/local/mysql/bin/mysql -uroot -p123"


#sql="create database test"

#sql="create table test.user(id int unsigned auto_increment primary,name varchar(32),password varcher(32))"

#sql="insert into test.user(username,password) values(‘jack‘,‘pwd‘)"


#傳參

case $1 in

delete)

sql="delete from test.user where id=$2"

;;

insert)

sql="insert into test.user(username,password) values(‘$2‘,‘$3‘)"

;;

update)

sql="update test.user set username=‘$3‘,password=‘$4‘ where id=$2"

;;

select | *)

sql="select * from test.user order by id"

;;


esac


$conn -e "$sql"


---------------------------------------

shell apache

apache日誌分割

rotatelogs,cronolog

#! /bin/bash

#logcut.sh


yesterday=`date -d yesterday +%Y%m%d`


srclog="/usr/local/apache2/logs/access_log"


dstlog="/usr/local/apache2/logsbak/access_${yesterday}"


mv $srclog $dstlog  


pkill -1 httpd 


------------------------

cat access_xxxxxxxx.log|awk ‘{print $1}‘ |sort|uniq -c


--------------------------

#! /bin/bash

#mysql.sh


conn="/usr/local/mysql/bin/mysql -uroot -p123"


#sql="create table test.countab(id int unsigned auto_increment primary key,data varchar(100),ip varchar(100),num int)"


#sql="desc test.countab"


$conn -e "$sql"


-------

#! /bin.bash


yesterday=`date -d yesterday +%Y%m%d`


srclog="/usr/local/apache2/logs/access_log"


dstlog="/usr/local/apache2/logsbak/access_${yesterday}"


mv $srclog $dstlog  


pkill -1 httpd 


tmpfile=$$.txt # $$什麼鬼


cat access_xxxxxxxx.log|awk ‘{print $1}‘ |sort|uniq -c|awk ‘{print $1":"$2}‘ > $tmpfile


mysql="/usr/local/mysql/bin/mysql -uroot -p123"

for i in `cat $tmpfile`

do

ip=`echo $i|awk -F: ‘{print $1}‘`

num=`echo $i|awk -F: ‘{print $2}`

sql="insert into test.countab(data,ip,num) values(‘$yesterday‘,‘$ip‘,‘$num‘)"

done


rm -rf $tmpfile


sql="select * from test.countab order by id"

$mysql -e "$sql"

-------------------

crontab -e 

#每晚0點執行

00 00 * * * /mnt/xxx.sh


---------------------











mysql php-mysql shell-mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.