SQL选择语句

发布于 2024-10-11 04:35:44 字数 225 浏览 1 评论 0原文

在字符串数组中,我有可变数量的值。 如果我想选择与数组变量相等的所有记录,sql 语句会是什么样子。

它看起来会是这样的:

SELECT * FROM users WHERE username='"+ variable amount of elements in the String array like: Steven, Mike, John ..... +"'"

in a string array i have a variable amount of values.
how will an sql statement look if i want to select all the records that are equal with the array variables.

it will look something this:

SELECT * FROM users WHERE username='"+ variable amount of elements in the String array like: Steven, Mike, John ..... +"'"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

优雅的叶子 2024-10-18 04:35:44

您可能正在寻找 IN( ) 运算符。

SELECT * FROM users WHERE username IN ('chris', 'bob', 'bill');

You might be looking for the IN( ) operator.

SELECT * FROM users WHERE username IN ('chris', 'bob', 'bill');

如梦亦如幻 2024-10-18 04:35:44

这? (显然,“OR username='xxx' 会根据您需要的数量重复)

SELECT * FROM users WHERE username='item1' OR username='item2' OR username='item3'

This? (obviously the "OR username='xxx' is repeated for as many items as you require)

SELECT * FROM users WHERE username='item1' OR username='item2' OR username='item3'
不交电费瞎发啥光 2024-10-18 04:35:44

我上面的每一个都是对的!有多种方法可以做到这一点!简单的谷歌搜索“mysql语句变量”会产生大量帮助完整结果,包括:

http://www .daniweb.com/forums/thread126895.html

只需将您的数组视为末尾带有[数字]的标准变量即可!

两个提示来自于此:

谷歌/首先搜索您的问题,十有八九其他人也遇到了同样的问题并找到了可行的解决方案!

准备好在 5 分钟内查看此处的答案。这个论坛可能是您见过的最快的。唯一拖慢社区速度的就是打字! :P

希望有帮助,

安迪

Every one above me is corrrect! There are multiple ways of doing this! A simple google search for 'mysql statement variables' yield tons of help full results including:

http://www.daniweb.com/forums/thread126895.html

Just treat your array like a standard varible with [a number] on the end!

Two tips come from this:

Google / Search your problem first 9 times out of 10 someone else has had the same problem and found a working solution!

And be prepared to look at the answers on here with in 5 mins. This forums probably the quickest you'll ever see. The only thing that slows the community down is typing ! :P

Hope that Helps,

Andy

梦言归人 2024-10-18 04:35:44

另一种方法虽然效率较低,但如果解析字符串出现问题,可以使用...

DECLARE @listOfNames VARCHAR(2000)
SET @ListOfNames = 'JOE,KELLIE,PETE'

SELECT * FROM users WHERE charindex(","+userName+",",","+@listOfNames+",") > 0

这种方法比其他任何一个答案都慢,但可以让您免于使用动态 SQL 来基于名称列表构建 SQL 查询通过...

Another approach, although less efficient, can be used if parsing the string apart is problematic...

DECLARE @listOfNames VARCHAR(2000)
SET @ListOfNames = 'JOE,KELLIE,PETE'

SELECT * FROM users WHERE charindex(","+userName+",",","+@listOfNames+",") > 0

This approach is slower than either of the other answers, but can save you from using dynamic SQL to build a SQL query based on the list of names passed in...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文