如何写SQL语句

whitehare 2002-01-17 01:34:33
TABEL1---->detail
item sa_code oper_code
--------------------------------
item1 001 002
item2 003 004

TABEL2--->person
id name
------------------
001 Mike
002 Boris
003 Rose
004 Tom

How write the SQL to display as

item sa_name oper_name
-------------------------------
item1 Mike Boris
item2 Rose Tom

...全文
186 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
whitehare 2002-01-21
  • 打赏
  • 举报
回复
结帐了,其实贴之一发出我就解决了,与BONBON_LI(待业青年)的方法相同。

方法总结:
1。用inner join 本人认为是最规范的用法。
2。当然从工程的角度出发,create view的方法比较好,这样可以在其它地方方便的使用而减少代码。
3。用delphi的特异功能,只可惜计算字段还没掌握。
4。就是qiubolecn(来自差生市)的用法,不过个人不喜欢这么用,会使程序可读性将底。

--方法好,查询语句不好
create view table1_v (item,sa_code,oper_code) as select t1.item,(select t2.name from table2 t2 where(t1.sa_code = t2.id)),(select t2.name from table2 t2 where (t1.oper_code = t2.id)) from table1 t1

SELECT detail.item, person.name AS sa_name, person_1.name AS oper_name
FROM (detail INNER JOIN person ON detail.sa_code = person.code) INNER JOIN person AS person_1 ON detail.oper_code = person_1.code;

1:用lookup的话,就是先在TABEL1---->detail中加两lookup字段sa_name oper_name
,分别用两TABLE对应,sa_name 其值要sa_code= id,且输出字段是name,oper_name也是一样的,这样能够解决,但比较死,要在设计期就做好。
2:计算字段的道理也差不多,只是在计算的事件中加入相应的计算规则:sa_name :=select name from preson,detail where sa_code=id

-- 这种方法在其他地方或许能派上用场,但来得太迟了,而且,前人已有用倒
select table1.item,(select name from table2 where table2.id=table1.sa_code) as 'sa_name',(select name from table2 where table2.id=table1.oper_code) as
'oper_name' from table1

qiubolecn 2002-01-20
  • 打赏
  • 举报
回复
晕,怎么少了一点

select table1.item,(select name from table2 where table2.id=table1.sa_code) as 'sa_name',(select name from table2 where table2.id=table1.oper_code) as
'oper_name' from table1
qiubolecn 2002-01-20
  • 打赏
  • 举报
回复
为何要用这么复杂????


一个语句搞定
select table1.item,(select name from table2 where table2.id=table1.sa_code) as sa_name,(select name from table2 where table2.id=table1.oper_code)

whitehare 2002-01-20
  • 打赏
  • 举报
回复
lookup会了,
计算字段的计算规则加在哪?

我会看情况加分
chenjiong 2002-01-18
  • 打赏
  • 举报
回复
来了,昨天论坛慢的不行,上了以后答不了。
1:用lookup的话,就是先在TABEL1---->detail中加两lookup字段sa_name oper_name
,分别用两TABLE对应,sa_name 其值要sa_code= id,且输出字段是name,oper_name也是一样的,这样能够解决,但比较死,要在设计期就做好。
2:计算字段的道理也差不多,只是在计算的事件中加入相应的计算规则:sa_name :=select name from preson,detail where sa_code=id
whitehare 2002-01-17
  • 打赏
  • 举报
回复
chenjiong(准程序员:升级中....)请说明
whitehare 2002-01-17
  • 打赏
  • 举报
回复


chenjiong<<<< < < < < < < < < <

你没事吧?
1898xxy 2002-01-17
  • 打赏
  • 举报
回复
试试
BONBON_LI 2002-01-17
  • 打赏
  • 举报
回复
SELECT detail.item, person.name AS sa_name, person_1.name AS oper_name
FROM (detail INNER JOIN person ON detail.sa_code = person.code) INNER JOIN person AS person_1 ON detail.oper_code = person_1.code;
武稀松 2002-01-17
  • 打赏
  • 举报
回复
create view table1_v (item,sa_code,oper_code) as select t1.item,(select t2.name from table2 t2 where(t1.sa_code = t2.id)),(select t2.name from table2 t2 where (t1.oper_code = t2.id)) from table1 t1
Access中视图叫查询
chenjiong 2002-01-17
  • 打赏
  • 举报
回复
cobi(我是小新) :是不是只有SQLSERVER支持视图?ACCESS支持不?
cobi 2002-01-17
  • 打赏
  • 举报
回复
考虑不要用sql实现,因为你的问题本身就可以通过创建视图实现,即table1中的两个数字用视图显示,table2作为table1的代码表提供视图

create view table1_v (item,sa_code,oper_code) as select t1.item,(select t2.name from table2 t2 where(t1.sa_code = t2.id)),(select t2.name from table2 t2 where (t1.oper_code = t2.id)) from table1 t1
chenjiong 2002-01-17
  • 打赏
  • 举报
回复
方法三:用计算字段。
方法四:临时表。
chenjiong 2002-01-17
  • 打赏
  • 举报
回复
方法一:用lookup字段。比较方便,但比较死。不过比较适合你现在的情况。
方法二:你是用什么数据库?
whitehare 2002-01-17
  • 打赏
  • 举报
回复
@_@

5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧