我有一个叫做玩家的桌子如下:
First_Id Second_Id Name
1 1 Durant
2 1 Kobe
1 2 Lebron
2 2 Dwight
1 3 Dirk
Run Code Online (Sandbox Code Playgroud)
我希望在这个表上写一个select语句来检索其第一个id和第二个id匹配一堆指定的第一个和第二个id的所有行.
因此,例如,我希望选择第一个和第二个ID如下的所有行:(1,1),(1,2)和(1,3).这将检索以下3行:
First_Id Second_Id Name
1 1 Durant
1 2 Lebron
1 3 Dirk
Run Code Online (Sandbox Code Playgroud)
是否可以以下列方式编写选择查询:
SELECT *
FROM PLAYERS
WHERE (First_Id, Second_Id) IN ((1,1), (1,2) and (1,3))?
Run Code Online (Sandbox Code Playgroud)
如果有一种方法可以编写类似于上面的SQL我想知道.有没有办法为IN子句指定表示多行的值,如图所示.
我正在使用DB2.
我有一个数据库,其中有四列对应于起始位置和结束位置的地理坐标x,y.列是:
我有这四列的索引,序列为x0,y0,x1,y1.
我列出了大约一百个地理对的组合.我如何有效地查询这些数据?
我想按照这个SO答案的建议做这样的事情,但它只适用于Oracle数据库,而不适用于MySQL:
SELECT * FROM my_table WHERE (x0, y0, x1, y1) IN ((4, 3, 5, 6), ... ,(9, 3, 2, 1));
Run Code Online (Sandbox Code Playgroud)
我以为可能有可能对索引做点什么?什么是最好的方法(即:最快的查询)?谢谢你的帮助!
笔记:
编辑:
代码按原样实际工作,但它非常慢,并没有利用索引(因为我们有一个旧版本的MySQL v5.6.27).
我有这样的查询,我试图通过比较元组来过滤结果集(如IN子句中的SQL多列):
select *
from mytable
where (key, value) in (values
('key1', 'value1'),
('key2', 'value2'),
...
);
Run Code Online (Sandbox Code Playgroud)
这是有效的语法,并在我的Postgres 9.3数据库上正常工作.
我想通过Spring JDBC调用此查询,其中in值对来自a List<Map<String,String>>.
做这样的事情会很好:
List<Map<String, String>> valuesMap = ...;
String sql = "select * from mytable where (key, value) in (values :valuesMap)";
SqlParameterSource params = new MapSqlParameterSource("valuesMap", valuesMap);
jdbcTemplate.query(sql, params, rowMapper);
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我得到:
org.postgresql.util.PSQLException: No hstore extension installed.
at org.postgresql.jdbc2.AbstractJdbc2Statement.setMap(AbstractJdbc2Statement.java:1707) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1910) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:36) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:47) ~[postgresql-9.3-1101-jdbc41.jar:na]
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:427) ~[spring-jdbc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:235) ~[spring-jdbc-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:150) …Run Code Online (Sandbox Code Playgroud) sql ×2
database ×1
db2 ×1
in-clause ×1
java ×1
mysql ×1
postgresql ×1
spring-jdbc ×1
where-in ×1