mysql多表查询插入更新_Mysql多表查询,多表插入和多表更新

/*************************************by

garcon1986*****************************************************/

多表查询:

CREATE TABLE IF NOT EXISTS contact(

contact_id int(11) NOT NULL AUTO_INCREMENT,

user_name varchar(255),

nom varchar(255),

prenom varchar(255),

mail varchar(64),

passcode char(64),

PRIMARY KEY(contact_id)

);

CREATE TABLE IF NOT EXISTS droit(

droit_id int( 11 ) NOT NULL AUTO_INCREMENT ,

droit varchar(255),

PRIMARY KEY(droit_id)

);

CREATE TABLE IF NOT EXISTS contactdroit(

contactdroit_id int(11) NOT NULL AUTO_INCREMENT,

contact_id int( 11 ),

droit_id int( 11 ),

PRIMARY KEY( contactdroit_id )

);

Insert into contact(contact_id, user_name) values(1,'user1');

Insert into contact(contact_id, user_name) values(2,'user2');

Insert into contact(contact_id, user_name) values(3,'user3');

Insert into droit(droit_id, droit) values(1,'admin');

Insert into droit(droit_id, droit) values(2,'superuser');

Insert into contactdroit(contact_id, droit_id) values(1, 1);

Insert into contactdroit(contact_id, droit_id) values(2, 1);

Insert into contactdroit(contact_id, droit_id) values(3, 2);

SELECT c.contact_id, d.droit_id, d.droit FROM contact c, contactdroit

cd, droit d

where c.contact_id = cd.contact_id

and cd.droit_id = d.droit_id;

结果:

contact_id droit_id droit

1 1 admin

2 1 admin

3 2 superuser

多表联查例子:

两个方法都可以,inner join on 更好点。

表结构没贴出来,但比较好懂了。

简单方法:

select c.nom, e.nom

from consultant c, affaire a, besoin b, salarie sa, site s, entreprise e

where c.consultant_id=a.consultant_id and a.besoin_id=b.besoin_id and

b.salarie_id=sa.salarie_id and sa.site_id=s.site_id and

s.entreprise_id=e.entreprise_id

inner join方法:

select c.nom, e.nom

from consultant c

inner join affaire a on c.consultant_id=a.consultant_id

inner join besoin b on a.besoin_id=b.besoin_id

inner join salarie sa on b.salarie_id=sa.salarie_id

inner join site s on sa.site_id=s.site_id

inner join entreprise e on s.entreprise_id=e.entreprise_id

多表插入:

$conn = mysql_connect("localhost","charles","charles");

mysql_select_db("test");

$query = "INSERT INTO contact(user_name,nom, prenom, mail, passcode)

values('sa','se','sf', 'safd@p.com', '123')";

$result = mysql_query($query) or die("insert contact

failed:".mysql_error());

$lastid = mysql_insert_id();

//得到上一个 插入的id值

echo "last insert id :".$lastid."
";

$query2 = "INSERT INTO contactdroit(contact_id, droit_id)

values('$lastid','11')";

echo $query2."
";

$result2 = mysql_query($query2) or die("insert contactdroit failed:

".mysql_error());

if(isset($result) && isset($result2)){

echo "Good Insertion
";

echo $lastid;

}

?>

需注意的是:

The mysql_insert_id() function returns the AUTO_INCREMENT ID

generated from the previous INSERT operation.

mysql_insert_id()函数的作用是:取得上一步 INSERT 操作产生的 ID。

This function returns 0 if the previous operation does not generate

an AUTO_INCREMENT ID, or FALSE on MySQL connection failure.

如果先前的操作不产生一个自动增加的ID[AUTO_INCREMENT ID],那么,函数返回0;如果MySQL连接失败,将返回False。

多表更新:

update contact c, contactdroit cd

set c.user_name = '$username', c.nom = '$lastname', c.prenom =

'$firstname', c.passcode = '$password', cd.droit_id = '$droitid'

where c.contact_id = '$id' and c.contact_id = cd.contact_id;

示例:

【以下为引用:http://www.javaeye.com/problems/340 】

mysql> create table one(id int(10), name varchar(20));

Query OK, 0 rows affected (0.03 sec)

mysql> create table two(id int(10), name varchar(20));

Query OK, 0 rows affected (0.05 sec)

mysql> insert one value(1, '1');

Query OK, 1 row affected (0.00 sec)

mysql> insert two value(22, '22');

Query OK, 1 row affected (1.02 sec)

mysql> update one o, two t set o.name='oo', t.name='tt';

Query OK, 2 rows affected (0.00 sec)

Rows matched: 2 Changed: 2 Warnings: 0

mysql> select * from one;

+------+------+

| id | name |

+------+------+

| 1 | oo |

+------+------+

1 row in set (0.00 sec)

mysql> select * from two;

+------+------+

| id | name |

+------+------+

| 22 | tt |

+------+------+

1 row in set (0.00 sec)

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-02-12 23:42

浏览 796

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值