MYSQL如何进行sql like (sql查询结果)的查询

2024-12-14 05:52:46
推荐回答(5个)
回答1:

select *
from table1
where
`text` like CONCAT( '%' , (select name from table2 where id =3), '%' );

这样看看, 行么?

CONCAT 是 mysql 中函数, 用于连接字符串的。

CREATE TABLE table1 (
`text` varchar(10)
);

CREATE TABLE table2 (
id int,
name varchar(10)
);

INSERT INTO table1 VALUES ('我爱家');
INSERT INTO table2 VALUES (3, '爱');

select *
from table1
where
`text` like CONCAT('%', (select name from table2 where id =3), '%' );

+--------+
| text |
+--------+
| 我爱家 |
+--------+
1 row in set (0.00 sec)

回答2:

select * from table1 where text(table1的字段) like '%(select name from table2 where id = 3 and rownum <2)%'

select name from table2 where id = 3 这句话查询的结构必须是一行或是空行,否则就不对。

回答3:

select * from table1 where text like (select name from table2 where id =3);
这样子就对了。text应该是一个字段名。

回答4:

你语法错了,试试
SELECT * FROM table1 WHERE `text` LIKE '%' + (SELECT name FROM table2 WHERE id =3) + '%';

回答5:

select *
from table1
where
`text` like CONCAT( '%' , (select name from table2 where id =3), '%' );

这样看看, 行么?

CONCAT 是 mysql 中函数, 用于连接字符串的。

CREATE TABLE table1 (
`text` varchar(10)
);

CREATE TABLE table2 (
id int,
name varchar(10)
);

INSERT INTO table1 VALUES ('我爱家');
INSERT INTO table2 VALUES (3, '爱');

select *
from table1
where
`text` like CONCAT('%', (select name from table2 where id =3), '%' );

+--------+
| text |
+--------+
| 我爱家 |
+--------+
1 row in set (0.00 sec)