[英]MYSQL: How to insert rows in a table based on a condition of other two tables
[英]How to map rows of two tables to each other based on the relevance in MySQL?
我有以下我的數據庫表: courses
(體育類的整個數據), coursedata
(含復印件courses.title
和courses.description
-需要FULLTEXT
索引/搜索的相關性), sports
(體育的列表),並courses_sports
(關聯表)-參見下文。
現在,我想映射與運動相關的課程,並自動用此數據填充courses_sports
。 它需要兩個步驟。
使用適當的SELECT
收集數據。
將數據寫入關聯表。
這篇文章是關於第一步的。 我在編寫查詢時遇到了一些麻煩。 我試過的
SELECT
courses.id,
sports.id
FROM
courses
JOIN
coursedata ON coursedata.id = courses.coursedata_id
JOIN
sports ON MATCH (coursedata.title) AGAINST (sports.title) > 0
-- The test with
-- sports ON MATCH (coursedata.title) AGAINST ('Basketball') > 0
-- works.
該查詢不起作用:
錯誤代碼:1210
AGAINST的參數不正確
如何正確實現此映射?
附加信息:相關表格
courses
Field Type Key
------------------ --------------- ------
id int(11) PRI
title varchar(100)
description varchar(1000)
coursedata_id int(11) UNI
...
coursedata
Field Type Collation Null Key
----------- ------------- --------------- ------ ------
id int(11) (NULL) NO PRI
title varchar(100) utf8_general_ci YES MUL
description varchar(1000) utf8_general_ci YES MUL
CREATE TABLE `coursedata` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) DEFAULT NULL,
`description` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `searchcoursetitle` (`title`),
FULLTEXT KEY `searchcoursedescription` (`description`)
) ENGINE=MyISAM AUTO_INCREMENT=5208 DEFAULT CHARSET=utf8
sports
Field Type Collation Null Key
-------- --------------------- --------------- ------ ------
id int(11) (NULL) NO PRI
title varchar(50) utf8_general_ci NO
category varchar(50) utf8_general_ci YES
type enum('sport','dance') utf8_general_ci YES
courses_sports
Field Type Collation Null Key
--------- ------- --------- ------ ------
course_id int(11) (NULL) NO PRI
sport_id int(11) (NULL) NO PRI
您忘記提供運動和課程之間的共同領域,在這種情況下,您將加入。 您還忘記了MATCH之前的WHERE語句。
JOIN
sports ON MATCH (coursedata.title) AGAINST (sports.title) > 0 AND
sports ON MATCH (coursedata.description) AGAINST (sports.title) > 0
因此,應該是這樣的:
JOIN
sports ON (course.commonid = sports.commonid) WHERE MATCH
而且,由於您同時需要coursedata.title和coursedata.description,因此可以將它們加入比賽。
JOIN
sports ON (course.commonid = sports.commonid) WHERE MATCH(coursedata.title, coursedata.description)
最后,您不能在sports.title中使用該字段,因為這將意味着所有要比較的體育標題都可以遍歷並將其值放入AGAINST中。
JOIN
sports ON (course.commonid = sports.commonid) WHERE MATCH(coursedata.title, coursedata.description) AGAINST('Martial')
可能還可以使用BOOLEAN模式,因為如果您有50%或以上的AGAINST匹配項,則該模式將不起作用
JOIN
sports ON (course.commonid = sports.commonid) WHERE MATCH(coursedata.title, coursedata.description) AGAINST('Martial' IN BOOLEAN MODE)
我有一個SQL Fiddle示例,但只有兩個表,即Coursedata和sports,並在兩個表之間添加了一個公共字段。
最后,也許您不必加入運動桌,而是遍歷運動桌然后與之對抗? 也許您可以將所有結果都結合起來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.