I want to implement a search function in my application based on these tables.
I have 5 fields on search page
I want to create a search query from based upon these rules,
insights
. read_time
, (how many times the article has been read) article
. article_id
Pre-mature working working example is here
I am running following query to get but it is not complete https://leading-people.com/search
SELECT
article
.article_id
FROMarticle
WHEREarticle
.is_active
= '1' AND (content
LIKE '%%' ORtitle
LIKE '%%' ORtags
LIKE '%%') UNION ALL SELECTarticle
.article_id
FROMkeywords
INNER JOINarticle
WHEREarticle
.is_active
= '1' AND (article
.article_id
=keywords
.article_id
ANDkeywords
.keywordtext
LIKE '%%')
TABLE article
COLUMNS
article_id
(PRIMARY) is_active
title
themonth
theyear
TABLE article_author
Comments: This table is just for reference author details is in another table. So these id(s) are just for reference.
COLUMNS
article_author_id
(PRIMARY) author_id
article_id
TABLE article_categories
Comments: This table is just for reference categories details is in another table. So these id(s) are just for reference.
COLUMNS
article_categories_id
(PRIMARY) article_id
categories_id
TABLE insights
Comments: This table is just for reference categories details is in another table. So these id(s) are just for reference.
COLUMNS
insights_id
(PRIMARY) article_id
read_time
TABLE keyword
Comments: This table is just for reference categories details is in another table. So these id(s) are just for reference.
COLUMNS
keyword_id
(PRIMARY) article_id
keywordtext
Hope! I've formatted it correctly so everyone can understand!
This might help you in your quest:
$keyword_mysql= mysql_real_escape_string($keyword);
SELECT article.article_id
FROM article
LEFT JOIN insights ON(article.article_id=insights.article_id)
WHERE article.is_active = '1' AND (content LIKE '%{$keyword_mysql}%' OR title LIKE '%{$keyword_mysql}%' OR tags LIKE '%{$keyword_mysql}%')
UNION DISTINCT
SELECT article.article_id
FROM keywords INNER JOIN article WHERE article.is_active = '1' AND (article.article_id = keywords.article_id AND keywords.keywordtext LIKE '%{$keyword_mysql}%')
LEFT JOIN insights ON(article.article_id=insights.article_id)
ORDER BY insights.read_time
You also could escape underscores and percents in $keyword_mysql with a backslash.
I'm not sure whether the ordering will work if the read_time column is not part of the selected fields. If not, add it to the selected fields and ORDER BY read_time instead.
After struggling for 6,7 hours, I was amazed that it was so simple :-(
Here is the solution that I came up with
SELECT
article
.article_id
FROMarticle
WHERE $cond (content
LIKE '%$term%' ORtitle
LIKE '%$term%' ORtags
LIKE '%$term%') ANDarticle
.article_id
IN (SELECTarticle_categories
.article_id
FROMarticle_categories
WHEREcategories_id
LIKE '$cat') ANDarticle
.article_id
IN (SELECTarticle_author
.article_id
FROMarticle_author
WHEREauthor_id
LIKE '$author')UNION ALL SELECT
article
.article_id
FROMkeywords
INNER JOINarticle
WHERE $cond (article
.article_id
=keywords
.article_id
ANDkeywords
.keyword
LIKE '%$term%') ANDarticle
.article_id
IN (SELECTarticle_categories
.article_id
FROMarticle_categories
WHEREcategories_id
LIKE '$cat') ANDarticle
.article_id
IN (SELECTarticle_author
.article_id
FROMarticle_author
WHEREauthor_id
LIKE '$author')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.