在 phpmyadmin 和 SQL Fiddle 上执行时相同 SELECT 查询的不同结果
Posted
技术标签:
【中文标题】在 phpmyadmin 和 SQL Fiddle 上执行时相同 SELECT 查询的不同结果【英文标题】:Different Results for Same SELECT Query When Executing on phpmyadmin and SQL Fiddle 【发布时间】:2015-09-15 09:15:22 【问题描述】:我编写了一个查询以获取 MySQL 中所有 国家 的最新 tax强>表。在 phpmyadmin(localhost) 和 SQL Fiddle 中执行相同的查询。两种结果是不同的。但是 SQL Fiddle 会产生正确的结果。
我可以找到相同的问题 发表在 Stack Overflow 之前的 Link
我没有从帖子中得到任何帮助。我的问题是如何在 phpmyadmin(localhost) 上为我在下面写的 SELECT
查询获得正确的结果;我的SELECT
查询有问题吗?
选择查询:(我在 Phpmyadmin 和 SQL Fiddle 上都执行过)
SELECT
t1.id, t1.country,
t1.tax, t1.created_by,
t1.created_on, t1.modified_by, t1.modified_on
FROM tax t1
INNER JOIN
(
SELECT country, MAX(created_on) AS latest
FROM tax GROUP BY country
) t4 ON t1.created_on=t4.latest AND t1.country=t4.country;
表格
+-------------+--------------+------+-----+-------------------+-----------------
------------+
| Field | Type | Null | Key | Default | Extra
|
+-------------+--------------+------+-----+-------------------+-----------------
------------+
| id | int(11) | NO | PRI | NULL | auto_increment
|
| country | int(11) | NO | | NULL |
|
| tax | decimal(8,5) | NO | | NULL |
|
| created_by | int(11) | NO | | NULL |
|
| created_on | timestamp | NO | | CURRENT_TIMESTAMP |
|
| modified_by | int(11) | YES | | NULL |
|
| modified_on | timestamp | YES | | NULL | on update CURREN
T_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------
------------+
创建表查询:(我在 SQL Fiddle 中尝试过)
CREATE TABLE tax ( id int(11) NOT NULL AUTO_INCREMENT, country int(11) NOT NULL, tax decimal(8,5) NOT NULL, created_by int(11) NOT NULL, created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, modified_by int(11) DEFAULT NULL, modified_on timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id) );
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,7,51,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,7,51,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,7,49,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,7,50,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,7,48,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (6,9.45450,51,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (3,8.88900,49,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (2,9.08989,49,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (2,4.00087,49,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,1.88900,49,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (4,5.54656,51,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (5,7.45435,50,'2015-06-26 16:26:20');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,43.88776,46,'2015-06-26 17:30:18');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (2,5.67,46,'2015-06-26 17:39:12');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (12,0.009,46,'2015-06-26 17:48:35');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (11,3,46,'2015-06-26 17:55:14');
INSERT INTO tax(country, tax, created_by, created_on) VALUES (1,5,46,'2015-06-26 17:55:39');
请找SQL Fiddle Result @
结果在 phpmyadmin(localhost) 中产生
+----+---------+---------+------------+---------------------+-------------+-----
--------+
| id | country | tax | created_by | created_on | modified_by | modi
fied_on |
+----+---------+---------+------------+---------------------+-------------+-----
--------+
| 3 | 3 | 7.00000 | 49 | 2015-06-26 16:26:20 | NULL | NULL
|
| 5 | 5 | 7.00000 | 48 | 2015-06-26 16:26:20 | NULL | NULL
|
| 6 | 6 | 9.45450 | 51 | 2015-06-26 16:26:20 | NULL | NULL
|
| 7 | 3 | 8.88900 | 49 | 2015-06-26 16:26:20 | NULL | NULL
|
| 11 | 4 | 5.54656 | 51 | 2015-06-26 16:26:20 | NULL | NULL
|
| 12 | 5 | 7.45435 | 50 | 2015-06-26 16:26:20 | NULL | NULL
|
| 14 | 2 | 5.67000 | 46 | 2015-06-26 17:39:12 | NULL | NULL
|
| 15 | 12 | 0.00900 | 46 | 2015-06-26 17:48:35 | NULL | NULL
|
| 16 | 11 | 3.00000 | 46 | 2015-06-26 17:55:14 | NULL | NULL
|
| 17 | 1 | 5.00000 | 46 | 2015-06-26 17:55:39 | NULL | NULL
|
+----+---------+---------+------------+---------------------+-------------+-----
--------+
【问题讨论】:
获得不同结果的明显原因是您使用不同的源数据。在 phpmyadmin 的示例输出中,与 sql fiddle 一起使用的数据集中没有两行。 哦,我的坏。你说的对?现在我得到两个结果相同@jpw 能否帮我编写查询以获取每个country
的所有最新tax
而不会重复?
查询不就是这样吗?
@jpw 是但是。如果您在 Result Produced in phpmyadmin(localhost)
中看到 country
列,则表 ID 3 和 5 重复。我只想要每个country
的最新tax
。
【参考方案1】:
查看此链接以查看错误日志的内容。
这是非常错误的,因为这些结果甚至没有意义。 where can I find mysql logs in phpmyadmin?
还可以查看简单的选择语句是否正常工作。
【讨论】:
以上是关于在 phpmyadmin 和 SQL Fiddle 上执行时相同 SELECT 查询的不同结果的主要内容,如果未能解决你的问题,请参考以下文章