在mysql数据库中,我有以下数据:
id, phonenumber, location
1, 111222, NY
2, 222333, GB
....
现在我要扩展phonenumber字段(varchar) 例如,如果我扩展一个更多的数字, 然后期望数据将是:
id, phonenumber, location
1, 1112220, NY
2, 1112221, NY
3, 1112222, NY
4, 1112223, NY
5, 1112224, NY
6, 1112225, NY
7, 1112226, NY
8, 1112227, NY
9, 1112228, NY
10, 1112229, NY
11, 2223330, PH
12, 2223331, PH
...
这是添加一个更多数字的示例。 真实情况是我需要添加 4 更多的数字。 (添加后缀0000~9999)
通过运行少量sql命令可以快速实现此目的吗? 谢谢!
答案 0 :(得分:0)
您可以对包含2个电话号码的表格使用联合查询和交叉联接
select *, concat(phonenumber,n) new_phonenumber from t
cross join
( select 0 as n
union
select 1 as n
union
select 2 as n
union
select 3 as n
union
select 4 as n
union
select 5 as n
union
select 6 as n
union
select 7 as n
union
select 8 as n
union
select 9 as n
) t1
order by phonenumber, n
答案 1 :(得分:0)
你可以做到这一点,虽然在MySQL中有点痛苦。您需要生成用于扩展的值以下内容:
select concat(n1.n, n2.n, n3.n, n4.n)
from (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n1 cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n2 cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n3 cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n4;
您可以使用cross join
将其与查询结合使用,以获取所需的数据:
select id, concat(phonenumber, n1.n, n2.n, n3.n, n4.n) as phonenumber, location
from yourtable t cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n1 cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n2 cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n3 cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 0
) n4;
如果您希望将这些结果放在表格中,我建议您使用以下方法将其保存到另一个表格中:
create table as . . .
在查询之前。
Here是一个SQL小提琴。
答案 2 :(得分:0)
Please try this code
<?php
$con=mysql_connect("localhost","username","password");
$db=mysql_select_db("databasename");
$query="select * from table_name";
$result=mysql_query($query);
while($re=mysql_fetch_array($result))
{
echo $re[1];
}
?>
答案 3 :(得分:0)
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT i4.i * 1000 + i3.i * 100 + i2.i*10 +i1.i j
FROM ints i1,ints i2, ints i3, ints i4
ORDER
BY j DESC
LIMIT 1;
+------+
| j |
+------+
| 9999 |
+------+