这个简单的查询有什么问题?
我昨天做了一个非常简单的查询,但今天早上我不记得我是怎么做的,而且我尝试的任何方法都不起作用。
我想做一个简单的 SELECT COUNT(*)
然后更新表 TEST。我们想要列 start (table1) 中的值有多少位于 txStart 列和 txEnd 列(来自表 TEST)中的值之间。
单独的 SELECT COUNT(*)
效果很好。
mysql> SELECT COUNT(*) FROM table1, TEST where table1.start BETWEEN TEST.txStart AND TEST.txEnd;
+----------+
| COUNT(*) |
+----------+
| 95149 |
+----------+
1 row in set (0.03 sec)
更新
从未发生过。
mysql> UPDATE TEST
SET rdc_1ips =
SELECT COUNT(*) FROM table1, TEST WHERE table1.start between TEST.txStart AND TEST.txEnd;
错误 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“SELECT COUNT(*) FROM table1, TEST WHERE table1.start Between TEST.txStart A”附近使用的正确语法
这是 table1 和表 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 |
+------+-----------+--------------+-------+---------+---------+----------+
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;
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 |
+------+-----------+--------------+-------+---------+---------+----------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将子选择放在括号中。
另外,我会给内部“测试”表一个别名(只是为了确定),
但我不确定即使语法正确,这是否会起作用。当从要更新的同一个表中进行选择时,MySQL 有一些令人讨厌的限制。
Put the sub-select in brackets.
Additionally I would give the inner "test" table an alias (just to be sure)
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.