确定一个表中的字段是否与另一个表中的字段类似,如果是,则计数发生次数,如果小于3则返回该名称

时间:2013-04-09 14:17:56

标签: mysql select join

我在同一个数据库中有两个表格图像和交互。我想确定具有png文件名的image.images是否为例 1001_A01_1-4_5mM_3AT_Xgal_7d_W.cropped.resized.grey.png类似于plate_name.Interactions,它看起来像1001_A01,然后计算图像显示的次数。如果我想要有plate_name.Interactions返回的图像少于3个,我应该得到3个图像。我想用1个查询来做这个。

到目前为止,我刚刚尝试计算出现的次数,但这是失败的:

select plate_name.Interactions, count(*) as count from Interactions where plate_name.Interactions like image.images;

以下是相关表格:

mysql> desc images;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| image      | varchar(100) | NO   | MUL | NULL    |       |
| user_id    | varchar(50)  | YES  |     | NULL    |       |
| project_id | varchar(50)  | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+

mysql> desc Interactions;
+----------------------+-------------+------+-----+---------+-------+
| Field                | Type        | Null | Key | Default | Extra |
+----------------------+-------------+------+-----+---------+-------+
| plate_name           | varchar(25) | NO   | MUL | NULL    |       |
| plate_number         | int(11)     | NO   | MUL | NULL    |       |
| bait_sequence_name   | varchar(25) | NO   |     | NULL    |       |
| bait_gene_promoter   | varchar(25) | NO   | MUL | NULL    |       |
| array_coord          | varchar(25) | NO   |     | NULL    |       |
| transcriptor_factor  | varchar(25) | NO   |     | NULL    |       |
| orf_name             | varchar(25) | NO   |     | NULL    |       |
| y_coord              | varchar(25) | NO   | MUL | NULL    |       |
| x_coord              | varchar(25) | NO   | MUL | NULL    |       |
| orig_intensity_value | varchar(25) | NO   |     | NULL    |       |
| rc_intensity_value   | varchar(25) | NO   |     | NULL    |       |
| ptp_intensity_value  | varchar(25) | NO   |     | NULL    |       |
| z_score              | varchar(25) | NO   | MUL | NULL    |       |
| z_prime              | varchar(20) | YES  |     | NULL    |       |
| call_type            | varchar(25) | NO   |     | NULL    |       |
| bleed_over           | varchar(25) | NO   | MUL | NULL    |       |
| plate_median         | int(11)     | YES  | MUL | NULL    |       |
| bait_gene            | varchar(25) | NO   |     | NULL    |       |
| bait_prey_orf        | varchar(25) | NO   |     | NULL    |       |
| human_call           | varchar(25) | NO   |     | NULL    |       |
| modified_call        | varchar(25) | NO   |     | NULL    |       |
| duplicate_call       | varchar(25) | YES  |     | NULL    |       |
| user_id              | varchar(25) | YES  | MUL | NULL    |       |
| project_id           | varchar(25) | YES  | MUL | NULL    |       |
+----------------------+-------------+------+-----+---------+-------+

为回应Bobby的回答而添加:

+----+-------------+--------------+-------+---------------+--------------+---------+---    ---+---------+-----------------------------------------------------------+
| id | select_type | table        | type  | possible_keys | key          | key_len | ref  | rows    | Extra                                                     |
+----+-------------+--------------+-------+---------------+--------------+---------+---    ---+---------+-----------------------------------------------------------+
|  1 | SIMPLE      | images       | index | NULL          | image        | 208     | NULL |   19581 | Using where; Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | Interactions | range | plate_median  | plate_median | 5       |   NULL | 3714984 | Using where; Using join buffer                            |
+----+-------------+--------------+-------+---------------+--------------+---------+------+---------+-----------------------------------------------------------+

1 个答案:

答案 0 :(得分:0)

我认为您以不正确的格式编写了表名和列名。 如果我是对的,查询应该只返回少于3张图像的plate_name, 所以你可以使用GROUP BY和HAVING方法来实现它。

SELECT Iteractions.plate_name, count(*) as `count`
FROM Interactions, Images
WHERE Interactions.plate_name LIKE Images.image
GROUP BY Interactions.plate_name
HAVING count(*) < 3

请尝试,并告诉我,这不是你想要的......:)