我想在表上创建一个条件连接。
我已经检查了here。但是我也读到OUTER JOIN
可能不是最好的方法(表现明智),因为仍然会进行全表扫描?对使用UNION
的性能似乎也有不同看法。
根据下面的要求,我可以有条件地加入表格的方式是什么? PS。如果您的解决方案需要它,我也可以将其实现为存储过程,以创建更动态的SQL。
对于userfavorites.objecttype = 100的所有行,基于photos.objecttype
的值,我想加入另一个表。
如果objecttype
值为1,我想加入[locations]
,如果值为2,我想加入[persons]
。
作为输出,我想要以下列:
photos.title,photos.locpath,(persons.title或locations.title),(persons.friendlyurl或locations.friendlyurl),userfavorites.objectid,userfavorites.objecttype
我现在有了这个来获取单个用户的行:
SELECT * FROM userfavorites uf
INNER JOIN photos vp ON uf.objectid=vp.id
WHERE uf.objecttype=100 AND uf.userid='32DD30EB-1691-457B-9FF5-FC41D687E579'
DDL和插入语句:
CREATE TABLE [dbo].[photos](
[id] [int] NOT NULL,
[objectid] [int] NOT NULL,
[locpath] [nvarchar](150) NOT NULL,
[objecttype] [tinyint] NOT NULL,
[title] [nvarchar](50) NULL) ON [PRIMARY]
CREATE TABLE [dbo].[userfavorites](
[id] [int] IDENTITY(1,1) NOT NULL,
[userid] [uniqueidentifier] NOT NULL,
[objectid] [int] NOT NULL,
[objecttype] [tinyint] NOT NULL CONSTRAINT [DF_userfavorites_objecttype] DEFAULT ((0)),
CONSTRAINT [PK_userfavorites] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[persons](
[id] [int] NOT NULL,
[title] [nvarchar](80) NOT NULL,
[friendlyurl] [nvarchar](80) NULL,
CONSTRAINT [PK_persons_1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
CREATE TABLE [dbo].[locations](
[id] [int] NOT NULL,
[title] [nvarchar](80) NOT NULL,
[friendlyurl] [nvarchar](80) NULL,
CONSTRAINT [PK_locations_1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
USE [tt]
SET IDENTITY_INSERT [dbo].[userfavorites] ON
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'32dd30eb-1691-457b-9ff5-fc41d687e579', 41029, 100)
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'32dd30eb-1691-457b-9ff5-fc41d687e579', 41030, 100)
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'32dd30eb-1691-457b-9ff5-fc41d687e579', 40880, 100)
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'32dd30eb-1691-457b-9ff5-fc41d687e579', 40885, 100)
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'32dd30eb-1691-457b-9ff5-fc41d687e579', 40882, 100)
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'32dd30eb-1691-457b-9ff5-fc41d687e579', 4067, 100)
INSERT [dbo].[userfavorites] ([userid], [objectid], [objecttype]) VALUES (N'BC5EB8A7-FEC2-4932-9C67-AE5A35C4012B', 4067, 100)
SET IDENTITY_INSERT [dbo].[userfavorites] OFF
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (41029, 1, 1, N'hiltonsky.jpg', N'Overview')
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (41030, 1, 1, N'pool.jpg', N'Swimming')
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (40880, 2, 1, N'entrance.jpg', N'Lobby Entrance')
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (40885, 2, 1, N'room.jpg', N'Room view')
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (40882, 1, 2, N'zuck.jpg', N'Mark')
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (4067, 2, 2, N'gates.jpg', N'Bill')
INSERT [dbo].[photos] ([id], [objectid], [objecttype], [locpath], [title]) VALUES (50673, 3, 2, N'musk.jpg', N'Elon')
INSERT [dbo].[locations] ([id], [title], [friendlyurl]) VALUES (1, N'Hilton Hotel', 'hilton-hotel')
INSERT [dbo].[locations] ([id], [title], [friendlyurl]) VALUES (2, N'Marriot Hotel', 'marriot-hotel')
INSERT [dbo].[persons] ([id], [title], [friendlyurl]) VALUES (1, N'Mark Zuckerberg', 'mark-zuckerberg')
INSERT [dbo].[persons] ([id], [title], [friendlyurl]) VALUES (2, N'Bill Gates', 'bill-gates')
INSERT [dbo].[persons] ([id], [title], [friendlyurl]) VALUES (3, N'Elon Musk', 'elon-musk')
因此对于用户ID 32dd30eb-1691-457b-9ff5-fc41d687e579
,结果将是:
+----------------+---------------+-----------------+-----------------+----------+------------+
| phototitle | locpath | title | friendlyurl | objectid | objecttype |
+----------------+---------------+-----------------+-----------------+----------+------------+
| Overview | hiltonsky.jpg | Hilton Hotel | hilton-hotel | 1 | 1 |
| Swimming | pool.jpg | Hilton Hotel | hilton-hotel | 1 | 1 |
| Lobby Entrance | entrance.jpg | Marriot Hotel | marriot-hotel | 2 | 1 |
| Room view | room.jpg | Marriot Hotel | marriot-hotel | 2 | 1 |
| Mark | zuck.jpg | Mark Zuckerberg | mark-zuckerberg | 1 | 2 |
| Bill | gates.jpg | Bill Gates | bill-gates | 2 | 2 |
+----------------+---------------+-----------------+-----------------+----------+------------+
答案 0 :(得分:4)
外部连接没有任何问题,似乎是解决问题的直接方法。是否使用全表扫描取决于DBMS;它将决定最快的数据访问权限。
SELECT
vp.phototitle,
vp.locpath,
coalesce(l.title, p.title) as title,
coalesce(l.friendlyurl, p.friendlyurl) as friendlyurl,
uf.objectid,
uf.objecttype
FROM userfavorites uf
INNER JOIN photos vp ON uf.objectid = vp.id
LEFT JOIN locations l on vp.objecttype= 1 and vp.objectid = l.id
LEFT JOIN persons p on vp.objecttype= 2 and vp.objectid = p.id
WHERE uf.objecttype = 100 AND uf.userid = '32DD30EB-1691-457B-9FF5-FC41D687E579';
但是,如果表格人员和位置非常相似,您可能需要考虑将它们作为单个表格,并带有标记,表明记录是个人还是位置。这将使事情变得更容易并加快访问速度。
答案 1 :(得分:0)
with cte as
( SELECT * FROM userfavorites uf
INNER JOIN photos vp ON uf.objectid=vp.id
WHERE uf.objecttype=100 AND uf.userid='32DD30EB-1691-457B-9FF5-FC41D687E579' )
SELECT *
from cte
JOIN locations on cte.objecttype = 1 and cte.objectid = locations.id
union all
SELECT *
from cte
JOIN persons on cte.objecttype = 2 and cte.objectid = persons.id