Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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
Javascript Mysql选择包含字符串的行+;特定int列比较_Javascript_Mysql_Sql_Database - Fatal编程技术网

Javascript Mysql选择包含字符串的行+;特定int列比较

Javascript Mysql选择包含字符串的行+;特定int列比较,javascript,mysql,sql,database,Javascript,Mysql,Sql,Database,我发现了许多与我有关的问题,但仍然无法解决这个问题 在我的表中,有3列用整数值填充,3列用字符串值填充。我有几排 表结构示例: INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 | VALUE3 33 | 25 | 10 | "nice"| "hello"| "goodbye" --------------------------------------------------- 10 | 15 | 28 | "d

我发现了许多与我有关的问题,但仍然无法解决这个问题

在我的表中,有3列用整数值填充,3列用字符串值填充。我有几排

表结构示例:

  INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 | VALUE3
   33   |  25   |   10  | "nice"| "hello"| "goodbye"
  ---------------------------------------------------
   10   |  15   |   28  | "dice"| "hay"  | "bird"
我有一个字符串,用于根据值列选择行。我想要选择它的方式是包含,这意味着如果字符串是“llo”,我应该得到一行,其中至少有一个值(VALUE,VALUE2,VALUE3)包含“llo”(例如,将选择VALUE2中带有“hello”的行)

但是,如果两个不同的行具有包含该字符串的值列(如示例中的字符串为“ice”),我希望检索与该值关联的INT列较高的行。在这个示例中,由于字符串与VALUE1进行了比较,我应该比较上一列的INT_1与下一列的INT_1,并检索INT_1较高的行。(INT_1->VALUE1,INT_2->VALUE2,INT_3->VALUE3)

虽然不多,但我自己也能想到:

 SELECT * FROM my_table WHERE VALUE1 = "+string+" OR VALUE2= "+string+" OR VALUE3= "+string+"";
  • 当我有“+string+”这样的值时,我不确定应该如何包含“LIKE”来检查是否包含字符串
  • 当我有多行VALUE包含字符串时,我不知道如何将特定INT列与特定VALUE列进行比较
首先使用
UNION ALL
规范化您的表。这意味着每一行必须分成三行。每组一个(
INT_1值1
INT_2值2
INT_3值3
)。由于没有显式主键,因此需要包含所有列来标识源行

select t.*, 1 as position, INT_1 as i, VALUE1 as v from my_table t
union all
select t.*, 2 as position, INT_2 as i, VALUE2 as v from my_table t
union all
select t.*, 3 as position, INT_3 as i, VALUE3 as v from my_table t
结果:

| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |       v |
|-------|-------|-------|--------|--------|---------|----------|----|---------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 |    nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 |    dice |
|    33 |    25 |    10 |   nice |  hello | goodbye |        2 | 25 |   hello |
|    10 |    15 |    28 |   dice |    hay |    bird |        2 | 15 |     hay |
|    33 |    25 |    10 |   nice |  hello | goodbye |        3 | 10 | goodbye |
|    10 |    15 |    28 |   dice |    hay |    bird |        3 | 28 |    bird |
| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |    v |
|-------|-------|-------|--------|--------|---------|----------|----|------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 | nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 | dice |

现在将其放入子查询中,并在
v
列中使用
WHERE v LIKE“%ice%”
搜索字符串

select *
from (
    select t.*, 1 as position, INT_1 as i, VALUE1 as v from my_table t
    union all
    select t.*, 2 as position, INT_2 as i, VALUE2 as v from my_table t
    union all
    select t.*, 3 as position, INT_3 as i, VALUE3 as v from my_table t
) n
where v like '%ice%'
结果:

| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |       v |
|-------|-------|-------|--------|--------|---------|----------|----|---------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 |    nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 |    dice |
|    33 |    25 |    10 |   nice |  hello | goodbye |        2 | 25 |   hello |
|    10 |    15 |    28 |   dice |    hay |    bird |        2 | 15 |     hay |
|    33 |    25 |    10 |   nice |  hello | goodbye |        3 | 10 | goodbye |
|    10 |    15 |    28 |   dice |    hay |    bird |        3 | 28 |    bird |
| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |    v |
|-------|-------|-------|--------|--------|---------|----------|----|------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 | nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 | dice |

最后一步-使用
ORDER BY i DESC LIMIT 1
选择
i
中值最高的行:

select `INT_1`, `INT_2`, `INT_3`, `VALUE1`, `VALUE2`, `VALUE3`
from (
    select t.*, 1 as position, INT_1 as i, VALUE1 as v from my_table t
    union all
    select t.*, 2 as position, INT_2 as i, VALUE2 as v from my_table t
    union all
    select t.*, 3 as position, INT_3 as i, VALUE3 as v from my_table t
) n
where v like '%ice%'
order by i desc
limit 1
结果:

| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 |
|-------|-------|-------|--------|--------|---------|
|    33 |    25 |    10 |   nice |  hello | goodbye |

如果使用
HAVING
子句而不是
WHERE
,则查询可能会更短,因此不需要使用子查询。但随后会得到两列(
i
v
),您可能不需要它们。另一方面,它们可能是您需要的唯一列

select t.*, INT_1 as i, VALUE1 as v from my_table t union all
select t.*, INT_2 as i, VALUE2 as v from my_table t union all
select t.*, INT_3 as i, VALUE3 as v from my_table t
having v like '%ice%'
order by i desc
limit 1
还有一个可能会稍微提高性能的修改:

select t.*, INT_1 as i from my_table t where VALUE1 like '%ice%' union all
select t.*, INT_2 as i from my_table t where VALUE2 like '%ice%' union all
select t.*, INT_3 as i from my_table t where VALUE3 like '%ice%'
order by i desc
limit 1

首先使用
UNION ALL
规范化表。这意味着每一行必须分成三行。每组一个(
INT_1值1
INT_2值2
INT_3值3
)。由于没有显式主键,因此需要包含所有列来标识源行

select t.*, 1 as position, INT_1 as i, VALUE1 as v from my_table t
union all
select t.*, 2 as position, INT_2 as i, VALUE2 as v from my_table t
union all
select t.*, 3 as position, INT_3 as i, VALUE3 as v from my_table t
结果:

| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |       v |
|-------|-------|-------|--------|--------|---------|----------|----|---------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 |    nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 |    dice |
|    33 |    25 |    10 |   nice |  hello | goodbye |        2 | 25 |   hello |
|    10 |    15 |    28 |   dice |    hay |    bird |        2 | 15 |     hay |
|    33 |    25 |    10 |   nice |  hello | goodbye |        3 | 10 | goodbye |
|    10 |    15 |    28 |   dice |    hay |    bird |        3 | 28 |    bird |
| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |    v |
|-------|-------|-------|--------|--------|---------|----------|----|------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 | nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 | dice |

现在将其放入子查询中,并在
v
列中使用
WHERE v LIKE“%ice%”
搜索字符串

select *
from (
    select t.*, 1 as position, INT_1 as i, VALUE1 as v from my_table t
    union all
    select t.*, 2 as position, INT_2 as i, VALUE2 as v from my_table t
    union all
    select t.*, 3 as position, INT_3 as i, VALUE3 as v from my_table t
) n
where v like '%ice%'
结果:

| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |       v |
|-------|-------|-------|--------|--------|---------|----------|----|---------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 |    nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 |    dice |
|    33 |    25 |    10 |   nice |  hello | goodbye |        2 | 25 |   hello |
|    10 |    15 |    28 |   dice |    hay |    bird |        2 | 15 |     hay |
|    33 |    25 |    10 |   nice |  hello | goodbye |        3 | 10 | goodbye |
|    10 |    15 |    28 |   dice |    hay |    bird |        3 | 28 |    bird |
| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 | position |  i |    v |
|-------|-------|-------|--------|--------|---------|----------|----|------|
|    33 |    25 |    10 |   nice |  hello | goodbye |        1 | 33 | nice |
|    10 |    15 |    28 |   dice |    hay |    bird |        1 | 10 | dice |

最后一步-使用
ORDER BY i DESC LIMIT 1
选择
i
中值最高的行:

select `INT_1`, `INT_2`, `INT_3`, `VALUE1`, `VALUE2`, `VALUE3`
from (
    select t.*, 1 as position, INT_1 as i, VALUE1 as v from my_table t
    union all
    select t.*, 2 as position, INT_2 as i, VALUE2 as v from my_table t
    union all
    select t.*, 3 as position, INT_3 as i, VALUE3 as v from my_table t
) n
where v like '%ice%'
order by i desc
limit 1
结果:

| INT_1 | INT_2 | INT_3 | VALUE1 | VALUE2 |  VALUE3 |
|-------|-------|-------|--------|--------|---------|
|    33 |    25 |    10 |   nice |  hello | goodbye |

如果使用
HAVING
子句而不是
WHERE
,则查询可能会更短,因此不需要使用子查询。但随后会得到两列(
i
v
),您可能不需要它们。另一方面,它们可能是您需要的唯一列

select t.*, INT_1 as i, VALUE1 as v from my_table t union all
select t.*, INT_2 as i, VALUE2 as v from my_table t union all
select t.*, INT_3 as i, VALUE3 as v from my_table t
having v like '%ice%'
order by i desc
limit 1
还有一个可能会稍微提高性能的修改:

select t.*, INT_1 as i from my_table t where VALUE1 like '%ice%' union all
select t.*, INT_2 as i from my_table t where VALUE2 like '%ice%' union all
select t.*, INT_3 as i from my_table t where VALUE3 like '%ice%'
order by i desc
limit 1

这是一个可怕的数据结构。但有一种方法可以做到这一点

SELECT t.*
FROM my_table t
WHERE VALUE1 LIKE '%string%' OR VALUE2 LIKE '%string%' OR VALUE3 LIKE '%string%'
ORDER BY greatest( (case when VALUE1 LIKE '%string%' then int_1 else -1 end),
                   (case when VALUE1 LIKE '%string%' then int_2 else -1 end),
                   (case when VALUE1 LIKE '%string%' then int_3 else -1 end) ) desc
LIMIT 1;

这是一个可怕的数据结构。但有一种方法可以做到这一点

SELECT t.*
FROM my_table t
WHERE VALUE1 LIKE '%string%' OR VALUE2 LIKE '%string%' OR VALUE3 LIKE '%string%'
ORDER BY greatest( (case when VALUE1 LIKE '%string%' then int_1 else -1 end),
                   (case when VALUE1 LIKE '%string%' then int_2 else -1 end),
                   (case when VALUE1 LIKE '%string%' then int_3 else -1 end) ) desc
LIMIT 1;

你确定你用的是MySQL吗?你确定你用的是MySQL吗?没有试过。我做了上面的回答,但没有尝试。我做了上面的回答