作者cutekid (可爱小孩子)
看板Database
标题Re: [SQL ] 要同时搜寻三个表格
时间Tue May 16 15:35:23 2017
select *
from (
select id,category,index from A
union all
select id,category,index from B
union all
select id,category,index from C
) t1
where index = 'xxx'
※ 引述《nature23306 (pART)》之铭言:
: 标题: [SQL ] 要同时搜寻三个表格
: 时间: Tue May 16 14:46:17 2017
:
: 大家好,这几天试着钻研JOIN但还是无解....
:
: 快崩溃...OTZ
:
:
:
: --
:
: 先讲解资料表的内容
:
: 有A、B、C
:
: 各个表格里有三个栏位id, category, index
:
: id:每个表格里都是从1开始往上递增
:
: category:算是种类,A表格的就是A,B就是B...以此类推
:
: index:每一笔资料都不一样
:
:
: --
:
:
: 想做的是,得到一个index字串,要在三个表格中找到这个index是在哪个表格
:
: 找到後,抓他的id和category
:
:
: --
:
: 以下是我打的程式码
:
: "SELECT A.id, A.category, B.id, B.category, C.id, C.category
: FROM A JOIN B JOIN C
: WHERE A.index='%s', B.index='%s', C=index='%s' ", index;
:
:
: 先谢谢各位
:
: --
:
: 台湾拳王x炉石之光x五唬将台柱
:
: https://clips.twitch.tv/EphemeralSuccessfulLadiesAMPEnergy
:
: --
:
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.42.71.113
: ※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1494917180.A.1AD.html
: → streetbad: SELECT 'A' as t_name,id,category FROM A WHERE index 05/16 15:14
: → streetbad: = @str UNION SELECT 'B' as t_name,id,category FROM 05/16 15:14
: → streetbad: B WHERE index = @str UNION ..... 05/16 15:14
: → streetbad: 可以同时捞出表名做成列表 如果要排序最後+order就好 05/16 15:15
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 210.61.233.210
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1494920124.A.88A.html
1F:推 nature23306: 大哥 请忽视刚刚丢的水球 我发现新的问题了 05/16 16:14