如何获得MySQL的结果:[select * from mytable AS t]?

时间:2014-01-17 08:48:54

标签: mysql sql

我有这样的SQL:

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test
FROM
(SELECT * FROM mytable) AS m1,
(SELECT * FROM mytable) AS m2,
(SELECT * FROM mytable) AS m3,
(SELECT * FROM mytable) AS m4,
(SELECT * FROM mytable) AS m5
WHERE m1.id1=m2.id2 AND m1.id1=m3.id3 AND m1.id1=m4.id4 AND m1.id1=m5.id5;

表mytable是巨大的,但我需要从中获取所有信息,我需要多次使用子查询,问题是:如何保存查询结果(SELECT * FROM mytable),如:

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test
FROM
(SELECT * FROM mytable) AS m1,
m1 AS m2,
m1 AS m3,
m1 AS m4,
m1 AS m5
WHERE m1.id1=m2.id2 AND m1.id1=m3.id3 AND m1.id1=m4.id4 AND m1.id1=m5.id5;

非常感谢。

2 个答案:

答案 0 :(得分:1)

您应该使用JOIN

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test
FROM mytable m1
JOIN mytable m2 ON m1.id1 = m2.id2
JOIN mytable m3 ON m1.id1 = m3.id3
JOIN mytable m4 ON m1.id1 = m4.id4
JOIN mytable m5 ON m1.id1 = m5.id5

答案 1 :(得分:1)

正确的方法是使用JOIN并让优化器完成他的工作:

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test
  FROM mytable as m1
  JOIN mytable as m2 ON m1.id1=m2.id2
  JOIN mytable as m3 ON m1.id1=m3.id3
  JOIN mytable as m4 ON m1.id1=m4.id4
  JOIN mytable as m5 ON m1.id1=m5.id5 ;

使用子查询从表中选择所有数据是没有意义的。