CakePHP 与 MySQL 查询的问题

发布于 2024-11-17 19:34:11 字数 570 浏览 1 评论 0原文

我有两个表:

客户:id,名称 文本:id、文本

文本表中的字段文本可以包含客户名称。我想为每个文本找到名称包含在文本字段中的客户。例如:

CLIENTS
id | name
1  | John Smith
2  | Mark Jackson
3  | Ann Boo

TEXTS
id | text
1  | John Smith and Ann Boo are my best friends.
2  | I really don't like Mark Jackson and John Smith.

结果应该是:

RESULT
Text.id | Client.id | Client.name
1       | 1         | John Smith
1       | 3         | Ann Boo
2       | 1         | John Smith
2       | 2         | Mark Jackson

我希望你能帮助我:) 我想知道 find() 函数应该有哪些参数或者只是 mysql 查询

I have two tables:

Clients: id, name
Texts: id, text

Field text in Texts table can contain names of clients. I would like to find for each text, clients whose names are contained in text field. For example:

CLIENTS
id | name
1  | John Smith
2  | Mark Jackson
3  | Ann Boo

TEXTS
id | text
1  | John Smith and Ann Boo are my best friends.
2  | I really don't like Mark Jackson and John Smith.

The result should be:

RESULT
Text.id | Client.id | Client.name
1       | 1         | John Smith
1       | 3         | Ann Boo
2       | 1         | John Smith
2       | 2         | Mark Jackson

I hope you will help me :)
I want to know what parameters should find() function have or just mysql query

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2024-11-24 19:34:11
DECLARE @clients TABLE (ID int IDENTITY(1,1), name VARCHAR(20))
INSERT @clients (name)
SELECT 'John Smith' UNION ALL SELECT 'Mark Jackson' UNION ALL SELECT 'Ann Boo'

DECLARE @texts TABLE (ID int IDENTITY(1,1), [text] VARCHAR(200))

INSERT @texts ([text]) VALUES ('John Smith and Ann Boo are my best friends.')
INSERT @texts ([text]) VALUES ('I really don''t like Mark Jackson and John Smith.')

SELECT T.id  [Text.id], C.id [Client.id], C.name [Client.name] 
FROM @CLIENTS C 
JOIN @TEXTS T 
ON T.[text] LIKE '%' + C.NAME + '%'

RESULT

Text.id     Client.id   Client.name
----------- ----------- --------------------
1           1           John Smith
1           3           Ann Boo
2           1           John Smith
2           2           Mark Jackson
DECLARE @clients TABLE (ID int IDENTITY(1,1), name VARCHAR(20))
INSERT @clients (name)
SELECT 'John Smith' UNION ALL SELECT 'Mark Jackson' UNION ALL SELECT 'Ann Boo'

DECLARE @texts TABLE (ID int IDENTITY(1,1), [text] VARCHAR(200))

INSERT @texts ([text]) VALUES ('John Smith and Ann Boo are my best friends.')
INSERT @texts ([text]) VALUES ('I really don''t like Mark Jackson and John Smith.')

SELECT T.id  [Text.id], C.id [Client.id], C.name [Client.name] 
FROM @CLIENTS C 
JOIN @TEXTS T 
ON T.[text] LIKE '%' + C.NAME + '%'

RESULT

Text.id     Client.id   Client.name
----------- ----------- --------------------
1           1           John Smith
1           3           Ann Boo
2           1           John Smith
2           2           Mark Jackson
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文