SQL选择语句
在字符串数组中,我有可变数量的值。 如果我想选择与数组变量相等的所有记录,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能正在寻找 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');
这? (显然,“OR username='xxx' 会根据您需要的数量重复)
This? (obviously the "OR username='xxx' is repeated for as many items as you require)
我上面的每一个都是对的!有多种方法可以做到这一点!简单的谷歌搜索“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
另一种方法虽然效率较低,但如果解析字符串出现问题,可以使用...
这种方法比其他任何一个答案都慢,但可以让您免于使用动态 SQL 来基于名称列表构建 SQL 查询通过...
Another approach, although less efficient, can be used if parsing the string apart is problematic...
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...