我有一个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文本向用户发送电子邮件。
答案 0 :(得分:4)
尝试:
SELECT *
FROM feed, recipients
WHERE feed.title LIKE concat('%', recipients.suburb, '%')
ORDER BY pubDate DESC
答案 1 :(得分:1)
如果你有一个带有id的suburb
表,你会有更好的E-R设计。然后,您可以在suburb_id
表和feed
表中使用外键recipients
。这会在您要查找的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