针对我可以使用表MySql中的值在联接条件下使用IN吗?和在mysql中,使用什么函数可将值联结到一起构成单个值这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展android–RealmQu
针对我可以使用表MySql中的值在联接条件下使用IN吗?和在mysql中,使用什么函数可将值联结到一起构成单个值这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展android – Realm Query可以使用IN吗?、LINQ to SQL-具有多个联接条件的左外部联接、mysql or 条件可以使用索引而避免全表、mysql or条件可以使用目录而避免全表等相关知识,希望可以帮助到你。
本文目录一览:- 我可以使用表MySql中的值在联接条件下使用IN吗?(在mysql中,使用什么函数可将值联结到一起构成单个值)
- android – Realm Query可以使用IN吗?
- LINQ to SQL-具有多个联接条件的左外部联接
- mysql or 条件可以使用索引而避免全表
- mysql or条件可以使用目录而避免全表
我可以使用表MySql中的值在联接条件下使用IN吗?(在mysql中,使用什么函数可将值联结到一起构成单个值)
运算符IN
与值列表一起使用,但是您所拥有的是带有逗号分隔的值列表的字符串,因此您不能使用它。
您可以使用FIND_IN_SET()
:
SELECT t2.*
FROM table2 t2 INNER JOIN table1 t1
ON FIND_IN_SET(t2.id,t1.table2_ids)
WHERE t1.id = 1
请参见demo。
结果:
> id | name
> -: | :---
> 2 | Joe
> 3 | John
> 4 | Bill
> 5 | Bob
,
根据您的问题,似乎您正在寻找内部联接而不是左联接。如果表1的值是单独的记录而不是ID的串联列表,则左联接可能有意义。
除非有特殊原因需要进行左连接,否则您可以考虑使用以下方法:
注意table1.values在这里被视为字符串(这仍然是错误的设计)
SELECT table2.*
FROM table1,table2
WHERE
table2.id IN table1.values
and
table1.id=1
更好的设计是将表1分解成这样:
id,table2_id
1,2
1,3
1,4
1,5
1,6
然后您可以适当地使用左联接:
SELECT table2.*
FROM table1
LEFT JOIN table2
ON table1.table2_id=table2.id
WHERE
table1.id=1
android – Realm Query可以使用IN吗?
我有以下领域查询,但从阅读文档我没有看到可能进行IN查询.
我需要在包含该id的字符串或数组中搜索id.这可能吗?
示例代码:
Realm realmThread = Realm.getInstance(visnetawrap.appModel); RealmResults<PropertyObject> propResults = realmThread.where(PropertyObject.class).contains("propertyID","(5,7,10)").findAll();
解决方法
RealmQuery<PropertyObject> query = realm.where(PropertyObject.class); query.beginGroup(); for(int i = 0; i < propertyIDs.length - 1; i++) { query.equalTo("propertyID",propertyIDs[i]).or(); } query.equalTo("propertyID",propertyIDs[propertyIDs.length - 1]).endGroup(); RealmResults<PropertyObject> propResults = query.findAll();
LINQ to SQL-具有多个联接条件的左外部联接
我有以下SQL,我正尝试将其转换为LINQ:
SELECT f.valueFROM period as p LEFT OUTER JOIN facts AS f ON p.id = f.periodid AND f.otherid = 17WHERE p.companyid = 100
我已经看到了左外部连接(即into x from y in x.DefaultIfEmpty()
等)的典型实现,但是不确定如何引入其他连接条件(ANDf.otherid = 17
)
编辑
为什么AND f.otherid =17
条件是JOIN而不是WHERE子句的一部分?因为f
某些行可能不存在,所以我仍然希望包含这些行。如果在JOIN之后在WHERE子句中应用了条件-
那么我没有得到想要的行为。
不幸的是:
from p in context.Periodsjoin f in context.Facts on p.id equals f.periodid into fgfrom fgi in fg.DefaultIfEmpty()where p.companyid == 100 && fgi.otherid == 17select f.value
似乎等效于此:
SELECT f.valueFROM period as p LEFT OUTER JOIN facts AS f ON p.id = f.periodid WHERE p.companyid = 100 AND f.otherid = 17
这不是我所追求的。
答案1
小编典典您需要在致电之前介绍您的加入条件DefaultIfEmpty()
。我只会使用扩展方法语法:
from p in context.Periodsjoin f in context.Facts on p.id equals f.periodid into fgfrom fgi in fg.Where(f => f.otherid == 17).DefaultIfEmpty()where p.companyid == 100select f.value
或者您可以使用子查询:
from p in context.Periodsjoin f in context.Facts on p.id equals f.periodid into fgfrom fgi in (from f in fg where f.otherid == 17 select f).DefaultIfEmpty()where p.companyid == 100select f.value
mysql or 条件可以使用索引而避免全表
在某些情况下,or 条件可以避免全表扫描的。
1 .where 语句里面如果带有 or 条件,myisam 表能用到索引, innodb 不行。
1) myisam 表:
CREATE TABLE IF NOT EXISTS `a` (
`id` int(1) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`aNum` char(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
| 1 | SIMPLE | a | index_merge | PRIMARY,uid | PRIMARY,uid | 4,4 | NULL | 2 | Using union(PRIMARY,uid); Using where |
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
1 row in set (0.00 sec)
2) innodb 表:
CREATE TABLE IF NOT EXISTS `a` (
`id` int(1) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`aNum` char(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | a | ALL | PRIMARY,uid | NULL | NULL | NULL | 5 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
2 . 必须所有的 or 条件都必须是独立索引:
+-------+----------------------------------------------------------------------------------------------------------------------
| Table | Create Table
+-------+----------------------------------------------------------------------------------------------------------------------
| a | CREATE TABLE `a` (
`id` int(1) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`aNum` char(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
explain 查看:
mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | a | ALL | PRIMARY | NULL | NULL | NULL | 5 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
全表扫描了。
3. 用 UNION 替换 OR (适用于索引列)
通常情况下,用 UNION 替换 WHERE 子句中的 OR 将会起到较好的效果。对索引列使用 OR 将造成全表扫描.
注意,以上规则只针对多个索引列有效。如果有 column 没有被索引,查询效率可能会因为你没有选择 OR 而降低.
在下面的例子中,LOC_ID 和 REGION 上都建有索引.
高效:
select loc_id , loc_desc , region from location where loc_id = 10
union
select loc_id , loc_desc , region from location where region = "melbourne"
低效:
select loc_id , loc desc , region from location where loc_id = 10 or region = "melbourne"
如果你坚持要用 OR, 那就需要返回记录最少的索引列写在最前面.
4. 用 in 来替换 or
这是一条简单易记的规则,但是实际的执行效果还须检验,在 oracle8i 下,两者的执行路径似乎是相同的.
低效:
select…. from location where loc_id = 10 or loc_id = 20 or loc_id = 30
高效
select… from location where loc_in in (10,20,30);
mysql or条件可以使用目录而避免全表
在某些情况下,or条件可以避免全表扫描的。
?
1 .where 语句里面如果带有or条件, myisam表能用到索引, innodb不行。
1)myisam表:
?CREATE TABLE IF NOT EXISTS `a` (
? `id` int(1) NOT NULL AUTO_INCREMENT,
? `uid` int(11) NOT NULL,
? `aNum` char(20) DEFAULT NULL,
? PRIMARY KEY (`id`),
? KEY `uid` (`uid`)
) ENGINE=MyISAM? DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
| id | select_type | table | type??????? | possible_keys | key???????? | key_len | ref? | rows | Extra???????????????????????????????? |
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
|? 1 | SIMPLE????? | a???? | index_merge | PRIMARY,uid?? | PRIMARY,uid | 4,4???? | NULL |??? 2 | Using union(PRIMARY,uid); Using where |
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
1 row in set (0.00 sec)
2)innodb表:
CREATE TABLE IF NOT EXISTS `a` (
? `id` int(1) NOT NULL AUTO_INCREMENT,
? `uid` int(11) NOT NULL,
? `aNum` char(20) DEFAULT NULL,
? PRIMARY KEY (`id`),
? KEY `uid` (`uid`)
) ENGINE=InnoDB? DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
mysql>? explain select * from a where id=1 or uid =2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | a???? | ALL? | PRIMARY,uid?? | NULL | NULL??? | NULL |??? 5 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
2 .必须所有的or条件都必须是独立索引:
+-------+----------------------------------------------------------------------------------------------------------------------
| Table | Create Table
+-------+----------------------------------------------------------------------------------------------------------------------
| a???? | CREATE TABLE `a` (
? `id` int(1) NOT NULL AUTO_INCREMENT,
? `uid` int(11) NOT NULL,
? `aNum` char(20) DEFAULT NULL,
? PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
explain查看:
mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | a???? | ALL? | PRIMARY?????? | NULL | NULL??? | NULL |??? 5 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
全表扫描了。
?
3. 用UNION替换OR (适用于索引列)
? ? ? ?通常情况下, 用UNION替换WHERE子句中的OR将会起到较好的效果. 对索引列使用OR将造成全表扫描.?
? ? ? ?注意, 以上规则只针对多个索引列有效. 如果有column没有被索引, 查询效率可能会因为你没有选择OR而降低.?
?
? ? ? ?在下面的例子中, LOC_ID 和REGION上都建有索引.
? ? ? ?高效:?
?
- select?loc_id?,?loc_desc?,?region?from?location?where?loc_id?=?10???
- union???
- select?loc_id?,?loc_desc?,?region??from?location?where?region?=?"melbourne"???
? ? ?低效:?
- select?loc_id?,?loc?desc?,?region?from?location?where?loc_id?=?10?or?region?=?"melbourne"??
?
如果你坚持要用OR, 那就需要返回记录最少的索引列写在最前面.
?
4. 用in来替换or ?
? ? ?这是一条简单易记的规则,但是实际的执行效果还须检验,在oracle8i下,两者的执行路径似乎是相同的.
低效:?
select…. from location where loc_id = 10 or loc_id = 20 or loc_id = 30?
高效?
select… from location where loc_in ?in (10,20,30);
?
我们今天的关于我可以使用表MySql中的值在联接条件下使用IN吗?和在mysql中,使用什么函数可将值联结到一起构成单个值的分享已经告一段落,感谢您的关注,如果您想了解更多关于android – Realm Query可以使用IN吗?、LINQ to SQL-具有多个联接条件的左外部联接、mysql or 条件可以使用索引而避免全表、mysql or条件可以使用目录而避免全表的相关信息,请在本站查询。
本文标签: