我通过PHP使用mysql_query获得了一组我希望INSERT
成为对列的值,我在编写查询时遇到了麻烦。我希望实现类似于以下结果的东西,避免重复:
Column 1 || Column 2
John || John
John || Richard
John || Kim
Richard || Richard
Richard || John
Richard || Kim
Kim || Kim
Kim || John
Kim || Richard
注意:以上示例只是我希望在插入数据后如何查看列的示例。我会在VALUES
查询中将'John,Richard和Kim'声明为INSERT
,我只是不确定如何实现这一点。
我还没有接近成功,但是如果我接近成功的话,我会更新(我的查询)。
先谢谢你们,Rich。
答案 0 :(得分:2)
您需要交叉加入
select a.value as Column1, b.value as Column2
from yourTable a
cross join YourTable b
如果您将其插入另一个表格,您可以执行以下操作: -
insert into NewTable (col1, col2)
select a.value as Column1, b.value as Column2
from yourTable a
cross join YourTable b
答案 1 :(得分:1)
SELECT
a.a + CAST( b AS VARCHAR(2)) AS c
INTO Tablec
FROM tablea a
CROSS JOIN tableb AS b;
SELECT
CAST( b.b AS VARCHAR(2)) + a AS d
INTO Tabled
FROM tableb b
CROSS JOIN tablea AS a
;
SELECT c FROM tablec Union SELECT d FROM tabled
使用下面的sql fiddle Link
中的代码