奇怪的选择声明? [英] Weird select statement ?

查看:51
本文介绍了奇怪的选择声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Schedule的列表。它被设置为varchar(120)。

时间表可以同时包含几个项目,如''MO'',''WE'','FR''。


我正在尝试选择以下

,但我不能得到任何结果。

我正在使用MS Sql 2000.


SELECT *来自tblMail

其中''MO''IN(附表)


是这可能吗?


谢谢。


兰迪
http://members.aol.com/rsmeiner

I have a table with a column called Schedule. It''s setup as varchar(120).
Schedule can contain several items at once like ''MO'',''WE'',''FR''.

I am trying to do a select like the following
but I can''t get any results back.
I''m using MS Sql 2000.

SELECT * from tblMail
where ''MO'' IN (Schedule)

Is this possible ?

Thanks.

Randy
http://members.aol.com/rsmeiner

推荐答案

您的设计在关系方面是错误的。不要将分隔列表

放入列中。您应该将日程表标准化为一个单独的表格:


CREATE TABLE MailSchedules(mailid INTEGER NOT NULL REFERENCES tblMail

(mailid)/ * tblMail的假定密钥* /,mailschedule CHAR(2)NOT NULL CHECK

(mailschedule IN(''MO'',''WE'',''FR'')),PRIMARY KEY(mailid,mailschedule))


INSERT INTO MailSchedules(mailid,mailschedule)VALUES(123,''MO'')

INSERT INTO MailSchedules(mailid,mailschedule)VALUES(123, ''WE'')

INSERT INTO MailSchedules(mailid,mailschedule)VALUES(456,''WE'')


现在您的查询更容易了更高效:


SELECT M. *

来自tblMail AS M

加入MailSchedules AS S

ON M.mailid = S.mailid AND S.mailschedule =''MO''


此外,不要在生产代码中使用SELECT *。列出列名,那样

当表结构发生变化时,你的代码应该更容易维护。


希望这会有所帮助。


-

David Portas

SQL Server MVP

-
Your design is wrong in relational terms. Don''t ever put delimited lists
into a column. You should normalize your schedules into a separate table:

CREATE TABLE MailSchedules (mailid INTEGER NOT NULL REFERENCES tblMail
(mailid) /* Assumed key for tblMail */, mailschedule CHAR(2) NOT NULL CHECK
(mailschedule IN (''MO'',''WE'',''FR'')), PRIMARY KEY (mailid, mailschedule))

INSERT INTO MailSchedules (mailid, mailschedule) VALUES (123,''MO'')
INSERT INTO MailSchedules (mailid, mailschedule) VALUES (123,''WE'')
INSERT INTO MailSchedules (mailid, mailschedule) VALUES (456,''WE'')

Now your query is much easier and more efficient:

SELECT M.*
FROM tblMail AS M
JOIN MailSchedules AS S
ON M.mailid = S.mailid AND S.mailschedule = ''MO''

Also, don''t use SELECT * in production code. List the column names, that way
your code should be easier to maintain when the table structures change.

Hope this helps.

--
David Portas
SQL Server MVP
--




" RSMEINER" < RS ****** @ aol.comcrap>在消息中写道

news:20 *************************** @ mb-m25.aol.com。 ..

"RSMEINER" <rs******@aol.comcrap> wrote in message
news:20***************************@mb-m25.aol.com...
我有一个名为Schedule的列表。它的设置为varchar(120)。
时间表可以同时包含几个项目,如''MO'',''WE'','FR''。

我我试图像以下那样做选择
但我不能得到任何结果。
我正在使用MS Sql 2000.

SELECT * from tblMail < br'>''MO''IN(附表)

这可能吗?

谢谢。

Randy
http://members.aol.com/rsmeiner
I have a table with a column called Schedule. It''s setup as varchar(120).
Schedule can contain several items at once like ''MO'',''WE'',''FR''.

I am trying to do a select like the following
but I can''t get any results back.
I''m using MS Sql 2000.

SELECT * from tblMail
where ''MO'' IN (Schedule)

Is this possible ?

Thanks.

Randy
http://members.aol.com/rsmeiner




这是一种方式:


SELECT *

来自dbo.tblMail

WHERE Schedule LIKE' '%MO%''


或者如果表中的值是单引号:


WHERE Schedule LIKE''%'' ''MO'''%''


您在一列中有重复值的事实表明数据

模型可能不正确 - 您可能想要查看您的表格结构如果你可以更充分地将它标准化,那么就可以看到

,因为它会让事情变得更容易。


Simon



Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE ''%MO%''

Or if the values are in single quotes in the table:

WHERE Schedule LIKE ''%''''MO''''%''

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon



" RSMEINER" < RS ****** @ aol.comcrap>在消息中写道

news:20 *************************** @ mb-m25.aol.com。 ..

"RSMEINER" <rs******@aol.comcrap> wrote in message
news:20***************************@mb-m25.aol.com...
我有一个名为Schedule的列表。它的设置为varchar(120)。
时间表可以同时包含几个项目,如''MO'',''WE'','FR''。

我我试图像以下那样做选择
但我不能得到任何结果。
我正在使用MS Sql 2000.

SELECT * from tblMail < br'>''MO''IN(附表)

这可能吗?

谢谢。

Randy
http://members.aol.com/rsmeiner
I have a table with a column called Schedule. It''s setup as varchar(120).
Schedule can contain several items at once like ''MO'',''WE'',''FR''.

I am trying to do a select like the following
but I can''t get any results back.
I''m using MS Sql 2000.

SELECT * from tblMail
where ''MO'' IN (Schedule)

Is this possible ?

Thanks.

Randy
http://members.aol.com/rsmeiner




这是一种方式:


SELECT *

来自dbo.tblMail

WHERE Schedule LIKE' '%MO%''


或者如果表中的值是单引号:


WHERE Schedule LIKE''%'' ''MO'''%''


您在一列中有重复值的事实表明数据

模型可能不正确 - 您可能想要查看您的表格结构如果你可以更充分地将它标准化,那么就可以看到

,因为它会让事情变得更容易。


Simon



Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE ''%MO%''

Or if the values are in single quotes in the table:

WHERE Schedule LIKE ''%''''MO''''%''

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon

这篇关于奇怪的选择声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆