MySQL从多个数据库查询并随机选择

时间:2014-08-07 15:08:47

标签: mysql database

我想根据来自另外两个表(一个保存在不同数据库中)的信息从单个表中选择随机条目。表格如下:

1-在数据库A中称为" islands"包含:

  +-------------+------------+------+-----+---------+----------------+
        | Field       | Type       | Null | Key | Default | Extra          |
        +-------------+------------+------+-----+---------+----------------+
        | id          | int(11)    | NO   | PRI | NULL    | auto_increment |
        | chrom       | int(11)    | NO   |     | NULL    |                |
        | start       | int(11)    | NO   |     | NULL    |                |
        | end         | int(11)    | NO   |     | NULL    |               

索引是:

+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| islands |          0 | PRIMARY   |            1 | id          | A         |       15991 |     NULL | NULL   |      | BTREE      |         |               |
| islands |          1 | locations |            1 | line_string | A         |        NULL |       32 | NULL   |      | SPATIAL    |         |               |

要从这个数据库中选择我通常使用:

SELECT * FROM islands FORCE INDEX (locations)
WHERE MBRIntersects(GeomFromText('Linestring(1 120, 1 120)'), line_string)

2 - 在databaseB call" Context"包含:

+---------+---------+------+-----+---------+----------------+
| Field   | Type    | Null | Key | Default | Extra          |
+---------+---------+------+-----+---------+----------------+
| id      | int(11) | NO   | PRI | NULL    | auto_increment |
| chrom   | int(11) | NO   |     | NULL    |                |
| site    | int(11) | NO   |     | NULL    |                |
| context | char(3) | NO   |     | NULL    |                |

此表中只有4个可能的上下文(已编入索引)

3 - 在databaseB中称为"条目"包含:

+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| chrom       | int(11) | NO   | MUL | NULL    |                |
| site        | int(11) | NO   | MUL | NULL    |                |
| methylation | float   | NO   |     | NULL    |                |

我想从条目表中选择一个随机条目,该条目表位于上下文表中,上下文条目为" CpG",可以位于" islands"是否表(我有两个不同的搜索)。

更新

根据以下评论我正在使用

SELECT * FROM
(SELECT t.chrom, t.site, t.methylation, c.context 
FROM f1 as t INNER JOIN context as c on c.chrom = t.chrom AND c.site = t.site 
WHERE c.context = 'CpG'
) AS s LIMIT 2;

获得:

+-------+-----------+-------------+---------+
| chrom | site      | methylation | context |
+-------+-----------+-------------+---------+
|     1 |  10003735 |          69 | CpG     |
|     1 | 100063074 |       98.79 | CpG     |
+-------+-----------+-------------+---------+

我想现在将这些结果加入到岛屿表中,以便从第一部分找到岛屿区域内的站点(或不是)。我正在使用:

SELECT * FROM
(SELECT t.chrom, t.site, t.methylation, c.context 
FROM f1 as t INNER JOIN context as c on c.chrom = t.chrom AND c.site = t.site 
WHERE c.context = 'CpG'
) AS s
CROSS JOIN islands as i 
WHERE MBRINTERSECTS(GeomFromText('Linestring(s.chrom s.site, s.chrom s.site)'), i.Line_string)
LIMIT 2;

然而,这给了我一个空集(它不应该)。

0 个答案:

没有答案