开发者

What's wrong with this simple query?

I did a very simple query yesterday but this morning I couldn't remember how I did it, and whatever I tried doesn't work.

I want to do a simple SELECT COUNT(*) and then update table TEST. We want how many values from column start are (table1) are between the values in column txStart and column txEnd (from table TEST).

The SELECT COUNT(*) alone works well.

mysql> SELECT COUNT(*) FROM table1, TEST where table1.start BETWEEN TEST.txStart AND TEST.txEnd;
+----------+
| COUNT(*) |
+----------+
|    95149 |
+----------+
1 row in set (0.03 sec)    

The UPDATE never happened.

 mysql> UPDATE TEST 
       SET rdc_1ips =  
       SELECT COUNT(*) FROM table1, TEST WHERE table1.start between TEST.txStart AND TEST.txEnd;开发者_如何学JAVA

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT(*) FROM table1, TEST WHERE table1.start between TEST.txStart A' at line 1

Here is a preview of the table1 and table TEST

mysql> SELECT * from table1 limit 2;
+----+--------+------+---------+---------+------+-------+
| id | strand | chr  | start   | end     | name | name2 |
+----+--------+------+---------+---------+------+-------+
|  1 | -      | 1    | 2999997 | 3000096 | NULL | NULL  |
|  2 | +      | 1    | 2999998 | 3000097 | NULL | NULL  |
+----+--------+------+---------+---------+------+-------+


mysql> SELECT * FROM TEST;
+------+-----------+--------------+-------+---------+---------+----------+
| chr  | pos_start | name         | name2 | txStart | txEnd   | rdc_1ips |
+------+-----------+--------------+-------+---------+---------+----------+
| 1    |   3204575 | NM_001011874 | Xkr4  | 3204562 | 3661579 |        0 |
+------+-----------+--------------+-------+---------+---------+----------+


Put the sub-select in brackets. Additionally I would give the inner "test" table an alias (just to be sure)

SET rdc_1ips = 
   (SELECT COUNT(*) FROM table1, TEST t2 
    WHERE table1.start between t2.txStart AND t2.txEnd)

But I'm not sure if this will work even if the syntax is correct. MySQL has some obnoxious limitiations when it comes to selecting from the same table that you want to update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜