如何分解mysql字段值并在SELECT查询中使用

如何分解mysql字段值并在SELECT查询中使用,mysql,Mysql,如何在select查询中分解表的字段值 例如,我在表中有一个名为“坐标”的字段,其中包含纬度、经度 现在我想在select查询中使用这个纬度和经度 我可以分离这些值并在select查询中使用它吗?首先,注释是正确的:这违反了正常形式。始终将单独的数据存储在单独的列中-这将使您的生活更轻松 如果您试图编写一条select语句来解析坐标字段,并尝试对其中一个或两个部分进行过滤,那么您将得到一个运行速度非常慢的查询,因为该列上的索引将无法运行。相反,我建议编写一个查询,将该列一分为二,如下所示: al

如何在select查询中分解表的字段值

例如,我在表中有一个名为“坐标”的字段,其中包含纬度、经度

现在我想在select查询中使用这个纬度和经度


我可以分离这些值并在select查询中使用它吗?

首先,注释是正确的:这违反了正常形式。始终将单独的数据存储在单独的列中-这将使您的生活更轻松

如果您试图编写一条select语句来解析
坐标
字段,并尝试对其中一个或两个部分进行过滤,那么您将得到一个运行速度非常慢的查询,因为该列上的索引将无法运行。相反,我建议编写一个查询,将该列一分为二,如下所示:

alter table `your_table`
    add column `coordinate_x` int null;
alter table `your_table`
    add column `coordinate_y` int null;
update `your_table`
    set `coordinate_x` = substring(`coordinates`,1,locate(',',`coordinates`))
        ,`coordinate_y`= substring(`coordinates`,locate(',',`coordinates`)+1);
alter table `your_table`
    drop column `coordinates`;
alter table `your_table`
    modify column `coordinate_x` int not null;
alter table `your_table`
    modify column `coordinate_y` int not null;

然后,您可以索引
坐标x
坐标y
,使select语句快速运行。

首先,注释是正确的:这违反了正常形式。始终将单独的数据存储在单独的列中-这将使您的生活更轻松

如果您试图编写一条select语句来解析
坐标
字段,并尝试对其中一个或两个部分进行过滤,那么您将得到一个运行速度非常慢的查询,因为该列上的索引将无法运行。相反,我建议编写一个查询,将该列一分为二,如下所示:

alter table `your_table`
    add column `coordinate_x` int null;
alter table `your_table`
    add column `coordinate_y` int null;
update `your_table`
    set `coordinate_x` = substring(`coordinates`,1,locate(',',`coordinates`))
        ,`coordinate_y`= substring(`coordinates`,locate(',',`coordinates`)+1);
alter table `your_table`
    drop column `coordinates`;
alter table `your_table`
    modify column `coordinate_x` int not null;
alter table `your_table`
    modify column `coordinate_y` int not null;

然后你可以索引
coordinate\ux
coordinate\uy
,使select语句快速运行。

顺便说一句,据我所知,这违反了1NF()@Kel:+1,你完全正确!数据库模式应该包含2个字段——1个表示纬度,1个表示经度。请看这里@凯尔,我仍然认为你可以把一个点(x,y或lat,long)看作一个场。不一定违反Codd的规则?顺便说一句,据我所知,这违反了1NF()@Kel:+1,你完全正确!数据库模式应该包含2个字段——1个表示纬度,1个表示经度。请看这里@凯尔,我仍然认为你可以把一个点(x,y或lat,long)看作一个场。不一定要违反Codd的规则?