MySQL LIKE 表的通配符

发布于 2024-09-16 07:09:59 字数 518 浏览 7 评论 0原文

我有一个 PHP 文档,它将执行一些 MySQL 并回显它。

代码:
$query1 = "SELECT * FROM feed,recipients WHERE feed.title LIKE \"%recipients.suburb%"\ ORDER BY pubDate DESC";

recipients.suburb (table.column) 位于我的 MySQL 数据库中。我如何对此执行通配符?

这可行,但我不能对桌子这样做?

$query1 = "SELECT * FROM feed,recipients WHERE feed.title LIKE '%test%' ORDER BY pubDate DESC";

我想检查 feed.title 列> 并查看此文本是否位于 recipients.suburb 中。它用于最终向用户发送电子邮件的脚本,该脚本基于与用户位置相关的 MySQL 文本。

I have a PHP document which will execute some MySQL and echoes it back.

Code:
$query1 = "SELECT * FROM feed, recipients WHERE feed.title LIKE \"%recipients.suburb%"\ ORDER BY pubDate DESC";

recipients.suburb (table.column) is in my MySQL DB. How can I perform wildcards on this?

This works, but I can't do it for tables?

$query1 = "SELECT * FROM feed, recipients WHERE feed.title LIKE '%test%' ORDER BY pubDate DESC";

I'd like to check the column feed.title and see if any of this text is in recipients.suburb. It is for a script that will eventually email users, based on text from MySQL co-relating to the location of the user.

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

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

发布评论

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

评论(2

甜嗑 2024-09-23 07:09:59

尝试:

SELECT * 
FROM feed, recipients 
WHERE feed.title LIKE concat('%', recipients.suburb, '%')
ORDER BY pubDate DESC

Try:

SELECT * 
FROM feed, recipients 
WHERE feed.title LIKE concat('%', recipients.suburb, '%')
ORDER BY pubDate DESC
寒冷纷飞旳雪 2024-09-23 07:09:59

如果您有一个带有 id 的 suburb 表,那么您会得到更好的 ER 设计。然后,您可以在 feed 表和 recipients 表中拥有外键 suburb_id。这将在您在此处查找的 feedrecipient 之间创建多对多关系。

解决方案是使用联接将收件人与郊区相同的提要进行匹配。

这是我的建议:

CREATE TABLE IF NOT EXISTS `feed` (
  `feed_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) DEFAULT NULL,
  `suburb` BIGINT( 20 ) UNSIGNED NULL,
  `pubDate` date DEFAULT NULL,
  PRIMARY KEY (`feed_id`)
  UNIQUE KEY `feed_id` (`feed_id`)
);

CREATE TABLE IF NOT EXISTS `recipients` (
  `recipient_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `suburb` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`recipient_id`),
  UNIQUE KEY `recipient_id` (`recipient_id`)
);

CREATE TABLE IF NOT EXISTS `suburb` (
  `suburb_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`suburb_id`),
  UNIQUE KEY `suburb_id` (`suburb_id`)
);

并使用这个 SELECT 语句:

SELECT feed.title, recipients.name, suburb.name
FROM feed, recipients, suburb
WHERE feed.suburb = suburb.suburb_id
AND suburb.suburb_id = recipients.suburb
AND feed.title LIKE CONCAT(  '%', suburb.name,  '%' ) 
ORDER BY pubDate DESC

You would be better off with a better E-R design where you have a suburb table with an id. Then you can have foreign key suburb_id in both the feed table and the recipients table. This would create a many-to-many relationship between feed and recipient that you are looking for here.

The solution would then be to use joins to match recipients to feeds where the suburb is the same.

Here is what I would propose:

CREATE TABLE IF NOT EXISTS `feed` (
  `feed_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) DEFAULT NULL,
  `suburb` BIGINT( 20 ) UNSIGNED NULL,
  `pubDate` date DEFAULT NULL,
  PRIMARY KEY (`feed_id`)
  UNIQUE KEY `feed_id` (`feed_id`)
);

CREATE TABLE IF NOT EXISTS `recipients` (
  `recipient_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `suburb` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`recipient_id`),
  UNIQUE KEY `recipient_id` (`recipient_id`)
);

CREATE TABLE IF NOT EXISTS `suburb` (
  `suburb_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`suburb_id`),
  UNIQUE KEY `suburb_id` (`suburb_id`)
);

And use this SELECT statement:

SELECT feed.title, recipients.name, suburb.name
FROM feed, recipients, suburb
WHERE feed.suburb = suburb.suburb_id
AND suburb.suburb_id = recipients.suburb
AND feed.title LIKE CONCAT(  '%', suburb.name,  '%' ) 
ORDER BY pubDate DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文