【问题标题】:MySQL LIKE wildcard for table表的 MySQL LIKE 通配符
【发布时间】:2010-08-26 16:36:48
【问题描述】:

我有一个 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 中与用户位置相关的文本向用户发送电子邮件。

【问题讨论】:

  • 你说的 这是什么意思,但我不能对表格执行此操作?
  • 它适用于值,但不适用于变量。没有“测试”列,只是将其用作值。 “郊区”有一栏。我想用这个。

标签: mysql wildcard sql-like


【解决方案1】:

试试:

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

【讨论】:

  • 太好了,谢谢。完美运行。也会尝试js1568的建议。
【解决方案2】:

如果您有一个带有 id 的 suburb 表,那么您最好使用更好的 E-R 设计。然后你可以在feed 表和recipients 表中都有外键suburb_id。这将在您在这里寻找的feed 和recipient 之间创建多对多关系。

然后解决方案是使用连接将收件人与郊区相同的提要匹配。

这是我的建议:

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

【讨论】:

  • 好的,我可以尝试一下。从未创建过外键。
猜你喜欢
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 2014-06-24
  • 2011-01-06
  • 1970-01-01
  • 2013-04-20
相关资源
最近更新 更多