Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL中用原始数据搜索被截断的数据_Mysql_Database - Fatal编程技术网

如何在MySQL中用原始数据搜索被截断的数据

如何在MySQL中用原始数据搜索被截断的数据,mysql,database,Mysql,Database,下面是一个数据库结构来说明我的问题 当我添加一个名称超过10个字符的公司时,它将被截断。因此,当我搜索公司全名时,我找不到它 问题:如何获得本例中为10的«name»列的最大长度,以便能够截断并使用«SUBSTRING()»进行研究 注意:上下文不允许更改文件的最大长度 名字 你是说你在找类似的东西 SELECT * FROM company WHERE company = LEFT('something longer than 10 chars', 10); ? LEFT() 要获得v

下面是一个数据库结构来说明我的问题

当我添加一个名称超过10个字符的公司时,它将被截断。因此,当我搜索公司全名时,我找不到它

问题:如何获得本例中为10的«name»列的最大长度,以便能够截断并使用«SUBSTRING()»进行研究

注意:上下文不允许更改文件的最大长度 名字


你是说你在找类似的东西

SELECT * FROM company WHERE company = LEFT('something longer than 10 chars', 10);
?

  • LEFT()
要获得varchar列的长度,必须查询信息\u模式

SELECT character_maximum_length 
FROM information_schema.columns 
WHERE table_schema = 'your_schema'
AND table_name = 'company'
AND column_name = 'name';
因此,您的查询将变为

SELECT * 
FROM company 
WHERE company = LEFT('something longer than 10 chars', (
    SELECT character_maximum_length 
    FROM information_schema.columns 
    WHERE table_schema = 'your_schema'
    AND table_name = 'company'
    AND column_name = 'name';
)
);

最好的方法是查询
信息\u模式
。像这样的东西应该适合你

select character_maximum_length 
  from information_schema.columns 
 where table_schema = database()
   and table_name = 'company'
   and column_name = 'name'
您将得到一个具有所需长度的单行结果集

您可以使用如下(讨厌的!)查询模式将其作为查询的一部分

select company_id, name
  from company
  where name = substring('A long search string', 1, (
                    select character_maximum_length 
                      from information_schema.columns 
                     where table_schema = database()
                       and table_name = 'company'
                       and column_name = 'name') )
或者您可以使用会话变量尝试这一(更可读的)连续查询对

select @max :=  character_maximum_length 
  from information_schema.columns 
 where table_schema = database()
   and table_name = 'hotels'
   and column_name = 'name' ;

select company_id, name
  from company
  where name = substring('A long search string', 1, @max)

不,我想自动获得“10”,即最大长度的column@kenfire编辑了我的答案。
select @max :=  character_maximum_length 
  from information_schema.columns 
 where table_schema = database()
   and table_name = 'hotels'
   and column_name = 'name' ;

select company_id, name
  from company
  where name = substring('A long search string', 1, @max)