打印

[数据库] 求一个SQL查询语句

表1记录如下:
id   f1    f2
1    男   性别
2    女   性别
3   三班  班级
4  二班   班级
……

表2记录如下:
姓名     性别    班级
张三     1        3
李四     2        4
……
很显然,表2当中的性别下的值是关联表1字段f2下值为“性别”的id,表2当中的班级下的值是关联表1字段f2下值为“班级”的id,怎么作出以下结果的查询
姓名     性别    班级
张三     男         二班
李四     女        一班
……

[ 本帖最后由 maysoft 于 2008-8-12 14:32 编辑 ]
left join语句多用几次就出来了。
承接定制建站、在线系统,代售域名、邮局、主机,价格从优。
有意者站内PM。
新一代四无新人……
对不起斑竹,我没描述清楚问题,重新改了问题
好畸形的表结构。
不过多用left join一样可以出来。
承接定制建站、在线系统,代售域名、邮局、主机,价格从优。
有意者站内PM。
新一代四无新人……

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!
老大能说的详细点好吗?我也是第一次做这种结构的

TOP

设计表的时候..是怎么想的

select a.姓名 as 姓名,b.f1 as 性别,c.f1 as 班级
from
表2 as b left join 表1 as a on (a.id = b.性别)
表2 as c left join 表1 as d on (d.id = c.班级)

[ 本帖最后由 wuleying 于 2008-8-12 14:44 编辑 ]
银子博客
经典站长QQ群:16719484
第三届经典论坛灌水王选举(12月1日-12月31日) 敬请期待

TOP

姓名与性别存在一张表中..班级存在另一张表中..

表1的结构太2了..
银子博客
经典站长QQ群:16719484
第三届经典论坛灌水王选举(12月1日-12月31日) 敬请期待

TOP

是有点变态
SELECT t2.name,t1.f1 FROM t1 INNER JOIN t2 Where t2.sex=t1.id OR t2.class=t1.id
这样取出的结果是
name               f1
男三                 男                  
男三                 三班                 
男二                 男                  
男二                 二班                 
女三                 女                  
女三                 三班                 
女二                 女                  
女二                 二班   
继续研究....

TOP

这样的设计有它的好处,Magento(一开源商城程序)属性的一张表的结构和表一是一样的
这种数据库的设计称作EVA
引自《php|architect’s Guide to Programming Magento》
引用:
"Magento’s database design is one of its most controversial aspects. Key data are modeled using the Entity Attribute Value method (EAV). Utilizing an EAV modeling pattern allows for unlimited attributes on any product, category, customer, or order, but EAV also depletes a programmer’s ability to write ad-hoc queries against the data"
引自《php|architect’s Guide to Programming Magento》
引用:
“As you can see from looking at the chart, adding a new attribute to a user simply involves adding a new record in the eav_attribute table. Adding a new attribute does not involve altering tables to add any new columns. This opens the door for graphical interfaces to easily manage adding new attributes to most parts of the system, while keeping the database schema consistent across installations. Notice that the eav_attribute table has extra type information. In EAV systems, the key to having a flexible, workable system is meta-data about the attributes.  Adding more columns to eav_attribute  could allow for information about the attributes,like how attributes are grouped together, whether or not values are required, or if the attribute should be restricted to certain entity types (should you allow “first_name”  for a product, or just a user?).  
   One downside to working with an EAV database is that the table design seems too loose.   There is not one single source for the definition of a user that can easily be  ”
常规的表设计

EAV模式的表设计


[ 本帖最后由 irlvirus 于 2008-8-12 15:28 编辑 ]

TOP

是的,这样设计表是有好处,我也是第一次这样做,可是使用起来可不方便

TOP

回复 9# irlvirus 的帖子

我只知道数据库设计范式..

EVA不会把性别这种属性也单独存在另一张表中吧..
银子博客
经典站长QQ群:16719484
第三届经典论坛灌水王选举(12月1日-12月31日) 敬请期待

TOP

回复 11# wuleying 的帖子

是的,每个属性都是一个记录,每个属性一个表
现在最新版的Magento安装好有194张表

TOP

回复 6# wuleying 的帖子

老大,表2 语法错误 ,这个提示

TOP