有
Table_A
------------
id name
1 Name1
2 Name2Table_B
------------
a_id type value
1 width 100
1 height 100
2 width 50
2 height 80构造怎样的查询,可以得到:
id name width height
1 Name1 100 100
2 Name2 50 80
Table_A
------------
id name
1 Name1
2 Name2Table_B
------------
a_id type value
1 width 100
1 height 100
2 width 50
2 height 80构造怎样的查询,可以得到:
id name width height
1 Name1 100 100
2 Name2 50 80
解决方案 »
- 请问一下 mysql数据库添加删除更新的存储过程怎么样生成的啊
- 请问mysql 中 查询第5行到第10行数据 怎么写?帮帮忙啦
- mysql怎么创建自动作业?
- mysql数据库图片插入和读取
- mysql fetch row 为什么会取到空值。
- Mysqldump 多表联合条件数据导出如何设置
- mysql日志文件怎么生不成呢?
- 在phpmyadmin中的以下显示,其中的多余空间,是指什么啊,
- sql如果表里有这条数据就插入,没有就不插入
- 新手提问: mysql数据库中 对一条记录修改的同时 不希望有其他操作对这一行也有修改 这要怎么实现
- Mysql取24小时之内的记录怎么写?函数怎么不能用?请高手指点
- 关于mysql的全文索引中文的问题
select tablea.id , tablea.name, tw.value as width, th.value as heightfrom tablea,
(select * from tableb where type="width") as tw,
(select * from tableb where type="height") as thwhere
tablea.id=tw.id and table.id=th.id
INNER JOIN B B1 ON A.ID=B1.A_ID AND B1.TYPE='width'
INNER JOIN B B2 ON A.ID=B2.A_ID AND B1.TYPE='height'
from Table_A a inner join Table_B b1 on a.id=b1.id
inner join Table_B b2 on a.id=b2.id
where b1.type='width'
and b2.type='height'
INNER JOIN B B1 ON A.ID=B1.A_ID AND B1.TYPE='width'
INNER JOIN B B2 ON A.ID=B2.A_ID AND B2.TYPE='height'OR
SELECT A.*,B1.value AS width, B2.value AS height FROM A
INNER JOIN B B1 ON A.ID=B1.A_ID
INNER JOIN B B2 ON A.ID=B2.A_ID
WHERE B1.TYPE='width' AND B2.TYPE='height'
事实上Table_B上有更多的type类型按这个方法语句长一点不是问题,就不知道效率怎么样
当type很多的时候有没有更好的方法?
http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html
中有一些例子你可以参考一下
create table Table_A(
id int primary key,
name varchar(10)
);创建B表
create table Table_B(
a_id int references Table_A(id),
type varchar(10),
value int
);插入值:insert into Table_A values(1 ,"Name1");
insert into Table_A values(2 ,"Name2");insert into Table_B values(1,"width",100 );
insert into Table_B values(1,"height",100 );
insert into Table_B values(2,"width",50 );
insert into Table_B values(2,"height",80 );
查找上述的表:select a.id,a.name,b1.value as width,b2.value as height
from Table_A a inner join Table_B b1 on a.id=b1.a_id
inner join Table_B b2 on a.id=b2.a_id
where b1.type='width'
and b2.type='height';运行结果如下:+----+-------+-------+--------+
| id | name | width | height |
+----+-------+-------+--------+
| 1 | Name1 | 100 | 100 |
| 2 | Name2 | 50 | 80 |
+----+-------+-------+--------+
2 rows in set (0.00 sec)你可以自己试试!可以结贴了吧!