简体   繁体   中英

How can I Implement Search Query in Mysql

I want to implement a search function in my application based on these tables.

I have 5 fields on search page

  1. Input box - Keywords or any word in the title
  2. Author - Select Box (author_id will be passed to the function as value)
  3. Category Name - Select Box (category_id will be passed to the function as value)
  4. themonth - Select Box from 1 - 12
  5. theyear - Select Box from 2000 - 2012

I want to create a search query from based upon these rules,

  1. Results array will be sorted by insights . read_time , (how many times the article has been read)
  2. Only want to get the 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 FROM article WHERE article . is_active = '1' AND ( content LIKE '%%' OR title LIKE '%%' OR tags LIKE '%%') UNION ALL 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 '%%')

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 FROM article WHERE $cond ( content LIKE '%$term%' OR title LIKE '%$term%' OR tags LIKE '%$term%') AND article . article_id IN (SELECT article_categories . article_id FROM article_categories WHERE categories_id LIKE '$cat') AND article . article_id IN (SELECT article_author . article_id FROM article_author WHERE author_id LIKE '$author')

UNION ALL SELECT article . article_id FROM keywords INNER JOIN article WHERE $cond ( article . article_id = keywords . article_id AND keywords . keyword LIKE '%$term%') AND article . article_id IN (SELECT article_categories . article_id FROM article_categories WHERE categories_id LIKE '$cat') AND article . article_id IN (SELECT article_author . article_id FROM article_author WHERE author_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM