Php MySQL搜索帮助

Php MySQL搜索帮助,php,mysql,database,Php,Mysql,Database,我正在使用php/mysql进行搜索。我的表格是'height'和数据类型=varchar(10),其中包含(5ft2in,5ft3in,…等等)之类的值。搜索时,我得到了两个值-height1和height2,它们基本上是范围。如何在该表中搜索范围?假设-我将给出5ft 1in到5ft 10in的范围,并希望得到这些值之间的数据。我正在使用php。请帮我解决这个问题。谢谢 我建议您以不同的方式存储高度值。如果将其存储为浮点(5.1英尺等)或整数(63英寸等),则可以轻松比较这些值。使用mysq

我正在使用php/mysql进行搜索。我的表格是'height'和数据类型=varchar(10),其中包含(5ft2in,5ft3in,…等等)之类的值。搜索时,我得到了两个值-height1height2,它们基本上是范围。如何在该表中搜索范围?假设-我将给出5ft 1in5ft 10in的范围,并希望得到这些值之间的数据。我正在使用php。请帮我解决这个问题。谢谢

我建议您以不同的方式存储高度值。如果将其存储为浮点(5.1英尺等)或整数(63英寸等),则可以轻松比较这些值。使用mysql解析包含英尺和英寸的字符串要困难得多

例如:

选择*FROM height,其中rowHeight介于12和20之间

我建议您以不同的方式存储高度值。如果将其存储为浮点(5.1英尺等)或整数(63英寸等),则可以轻松比较这些值。使用mysql解析包含英尺和英寸的字符串要困难得多

例如:
SELECT*FROM height,其中rowHeight介于12和20之间

由于使用varchar()存储高度数据,因此高度值存储为字符串。因此,要获得某个范围内的值,必须首先对数据进行排序。但是由于您使用的是varchar(),所以当您按字母顺序对列进行排序时,值5ft 10in在5ft 1in之前

因此,我建议您将英尺值和英寸值作为整数存储在两列中,然后根据这两列对数据进行排序,使英尺优先,英寸优先

然后,在读取结果集时,只需将字符串“ft”和“in”连接到数据中

由于使用varchar()存储高度数据,因此高度值存储为字符串。因此,要获得某个范围内的值,必须首先对数据进行排序。但是由于您使用的是varchar(),所以当您按字母顺序对列进行排序时,值5ft 10in在5ft 1in之前

因此,我建议您将英尺值和英寸值作为整数存储在两列中,然后根据这两列对数据进行排序,使英尺优先,英寸优先


然后,在读取结果集时,只需将字符串“ft”和“in”连接到数据中

你真的应该像其他人说的那样,把它存储为一个整数值(英寸),所以5英尺10英寸是70英寸

要再次将其显示为ft/in,只需执行以下操作:

$result = 70; //Whatever is retrieved from the DB really, in inches
if($result >= 12) //If result is a foot or more
{
     $feet = floor($result / 12); //Divide the total inches by 12 and round down
     $inches = $result % 12; //Modulus total inches by twelve to get remainder inches
     $string = $feet . 'ft ' . $inches . 'in'; //Combine into string
}
else
{
     $string = $result . 'in'; //Unless you want it to display 0ft 7in or whatever if less than a foot
}
$sql = mysql_query("SELECT `id`,`height` FROM `table` WHERE 1");
while($row = mysql_fetch_assoc($sql))
{
     $values = explode(" ",$row['height']); //Split it into two parts at the space between feet and inches
     $feet = (int)$values[0];  //Could also trim off the last 2 characters in the string
     $inches = (int)$values[1];
     $total = ($feet * 12) + $inches; //Of course, given order of operations, the parentheses aren't necessary, but makes it look nicer
     mysql_query("UPDATE `table` SET `height` = $total WHERE `id` = $row['id']");
}
您可以很容易地将DB中的所有值转换为整数,只需使用一个简单的脚本即可从DB中提取数据,将英尺和英寸分开,修剪字符串部分(只要将它们分开后转换为int即可),然后将英尺的数字部分乘以12,将其添加到英寸的数字部分,并更新DB

看起来像这样:

$result = 70; //Whatever is retrieved from the DB really, in inches
if($result >= 12) //If result is a foot or more
{
     $feet = floor($result / 12); //Divide the total inches by 12 and round down
     $inches = $result % 12; //Modulus total inches by twelve to get remainder inches
     $string = $feet . 'ft ' . $inches . 'in'; //Combine into string
}
else
{
     $string = $result . 'in'; //Unless you want it to display 0ft 7in or whatever if less than a foot
}
$sql = mysql_query("SELECT `id`,`height` FROM `table` WHERE 1");
while($row = mysql_fetch_assoc($sql))
{
     $values = explode(" ",$row['height']); //Split it into two parts at the space between feet and inches
     $feet = (int)$values[0];  //Could also trim off the last 2 characters in the string
     $inches = (int)$values[1];
     $total = ($feet * 12) + $inches; //Of course, given order of operations, the parentheses aren't necessary, but makes it look nicer
     mysql_query("UPDATE `table` SET `height` = $total WHERE `id` = $row['id']");
}
当然,您要做的是在表上创建一个新的空列来保存新的整数值,如
heightint
或其他什么,然后将旧列重命名为其他列,然后将新列重命名为height

好的,如果您没有能力修改表(您真的应该向您的客户提出这样的改进建议),那么我认为您需要做的是运行如下查询:

SELECT * FROM `table` WHERE `height` >= '5ft 10in' AND `height` <= '5ft 1in'

从'table'中选择*,其中'height`>='5ft 10in'和'height`你真的应该像其他人说的那样,并将其存储为单个整数值(英寸),因此5ft 10in是70

要再次将其显示为ft/in,只需执行以下操作:

$result = 70; //Whatever is retrieved from the DB really, in inches
if($result >= 12) //If result is a foot or more
{
     $feet = floor($result / 12); //Divide the total inches by 12 and round down
     $inches = $result % 12; //Modulus total inches by twelve to get remainder inches
     $string = $feet . 'ft ' . $inches . 'in'; //Combine into string
}
else
{
     $string = $result . 'in'; //Unless you want it to display 0ft 7in or whatever if less than a foot
}
$sql = mysql_query("SELECT `id`,`height` FROM `table` WHERE 1");
while($row = mysql_fetch_assoc($sql))
{
     $values = explode(" ",$row['height']); //Split it into two parts at the space between feet and inches
     $feet = (int)$values[0];  //Could also trim off the last 2 characters in the string
     $inches = (int)$values[1];
     $total = ($feet * 12) + $inches; //Of course, given order of operations, the parentheses aren't necessary, but makes it look nicer
     mysql_query("UPDATE `table` SET `height` = $total WHERE `id` = $row['id']");
}
您可以很容易地将DB中的所有值转换为整数,只需使用一个简单的脚本即可从DB中提取数据,将英尺和英寸分开,修剪字符串部分(只要将它们分开后转换为int即可),然后将英尺的数字部分乘以12,将其添加到英寸的数字部分,并更新DB

看起来像这样:

$result = 70; //Whatever is retrieved from the DB really, in inches
if($result >= 12) //If result is a foot or more
{
     $feet = floor($result / 12); //Divide the total inches by 12 and round down
     $inches = $result % 12; //Modulus total inches by twelve to get remainder inches
     $string = $feet . 'ft ' . $inches . 'in'; //Combine into string
}
else
{
     $string = $result . 'in'; //Unless you want it to display 0ft 7in or whatever if less than a foot
}
$sql = mysql_query("SELECT `id`,`height` FROM `table` WHERE 1");
while($row = mysql_fetch_assoc($sql))
{
     $values = explode(" ",$row['height']); //Split it into two parts at the space between feet and inches
     $feet = (int)$values[0];  //Could also trim off the last 2 characters in the string
     $inches = (int)$values[1];
     $total = ($feet * 12) + $inches; //Of course, given order of operations, the parentheses aren't necessary, but makes it look nicer
     mysql_query("UPDATE `table` SET `height` = $total WHERE `id` = $row['id']");
}
当然,您要做的是在表上创建一个新的空列来保存新的整数值,如
heightint
或其他什么,然后将旧列重命名为其他列,然后将新列重命名为height

好的,如果您没有能力修改表(您真的应该向您的客户提出这样的改进建议),那么我认为您需要做的是运行如下查询:

SELECT * FROM `table` WHERE `height` >= '5ft 10in' AND `height` <= '5ft 1in'

SELECT*FROM`table`WHERE`height`>='5ft 10in'和`height`这会让生活变得复杂,但只要是个位数,你就可以在“inch”部分加上一个“0”。考虑到您正在存储高度,让我们忽略高度达到10英尺或更高的情况

样本数据

create table height(height varchar(20) collate utf8_general_ci);
insert height select '5ft 10in';
insert height select '6ft 1in';
insert height select '7ft 10in';
insert height select '8ft 1in';
insert height select '6ft 11in';
insert height select '7ft 2in';
选择
语句

select *
from height
cross join (select '5ft 9in' as low, '7ft 3in' as high) inputs
where
  case when height like '%ft _in' then
    concat(left(height, instr(height,'ft')+2), '0', right(height,3)) else height end
between
  case when low like '%ft _in' then
    concat(left(low, instr(low,'ft')+2), '0', right(low,3)) else low end
and
  case when high like '%ft _in' then
    concat(left(high, instr(high,'ft')+2), '0', right(high,3)) else high end
本质上,您将高度1和高度2输入插入此部件

cross join (select '$height1' as low, '$height2' as high) inputs

它使生活变得复杂,但只要是个位数,你就可以在“英寸”部分加上一个“0”。考虑到您正在存储高度,让我们忽略高度达到10英尺或更高的情况

样本数据

create table height(height varchar(20) collate utf8_general_ci);
insert height select '5ft 10in';
insert height select '6ft 1in';
insert height select '7ft 10in';
insert height select '8ft 1in';
insert height select '6ft 11in';
insert height select '7ft 2in';
选择
语句

select *
from height
cross join (select '5ft 9in' as low, '7ft 3in' as high) inputs
where
  case when height like '%ft _in' then
    concat(left(height, instr(height,'ft')+2), '0', right(height,3)) else height end
between
  case when low like '%ft _in' then
    concat(left(low, instr(low,'ft')+2), '0', right(low,3)) else low end
and
  case when high like '%ft _in' then
    concat(left(high, instr(high,'ft')+2), '0', right(high,3)) else high end
本质上,您将高度1和高度2输入插入此部件

cross join (select '$height1' as low, '$height2' as high) inputs

你真的应该把所有的值都存储在一个单位里,然后你可以转换并格式化显示。实际上我没有修改表的特权。我正在做一个零件。所以我必须在这种情况下做。你知道吗?你真的应该把所有的值都存储在一个单位里,然后你可以转换并格式化显示。实际上我没有修改表的特权。我正在做一个零件。所以我必须在这种情况下做。知道吗?事实上我没有修改表格的特权。我正在做一个零件。所以我