简体   繁体   English

从选择列表中选择查询多个值

[英]Select query on multiple values from select list

I have a select list that I can get the user IDs via: 我有一个选择列表,我可以通过以下列表获取用户标识:

$('#lstUsers :selected').each(function(i, selected){ 
   console.log ($(selected).val());
});

I need to gather all of those values and then run a subsequent query such that 我需要收集所有这些值,然后运行后续查询,以便

Select * from myTable where id = (id OR id2 OR id3... OR idN) 从myTable中选择*,其中id =(id或id2或id3 ...或idN)

What's a good way to accomplish this? 什么是实现此目的的好方法?

Satya's comment is correct that IN is probably faster and makes it easier to generate the SQL. 萨蒂亚(Satya)的评论是正确的,因为IN可能更快,并且更易于生成SQL。

But was your question more basic? 但是您的问题更基本吗? (Your js was not basic but the mysql question seems to be, so I'm not sure.) We're you asking how to query the server from a page running javascript? (您的js不是基本的,但mysql问题似乎是这样,所以我不确定。)我们在问如何从运行javascript的页面查询服务器? If so, the answer is an ajax call . 如果是这样,答案是ajax call The URL will have a script that runs and calls requests the data (perhaps using IN ) then outputs it (perhaps in JSON). 该URL将具有一个脚本,该脚本运行并调用请求数据(可能使用IN ),然后输出数据(可能使用JSON)。 Your js receives it and does what it needs to with the returned data. 您的js会收到它,然后对返回的数据执行所需的操作。

Could do something like this: 可以做这样的事情:

var ids = [];
$('#lstUsers :selected').each(function(i, selected){ 
    ids = ids.concat($(selected).val());
});
var query = "Select * from myTable where id in (" + ids.join(",") + ")";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM