1

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!

4

2 に答える 2

0

6,7時間格闘した後、とても簡単だったことに驚きました:-(

これが私が思いついた解決策です

選択しarticleます。article_idFROM articleWHERE $cond ( contentLIKE '%$term%' OR titleLIKE '%$term%' OR tagsLIKE '%$term%') AND article. article_idIN (SELECT article_categories. article_idFROM article_categoriesWHERE categories_idLIKE '$cat') AND article. article_idIN (SELECT article_author. article_idFROM article_authorWHERE author_idLIKE '$author')

UNION ALL SELECT article. article_idFROM keywordsINNER JOIN articleWHERE $cond ( article. article_id= keywords. article_idAND keywords. keywordLIKE '%$term%') AND article. article_idIN (SELECT article_categories. article_id FROM article_categoriesWHERE categories_idLIKE '$cat') AND article. article_idIN (SELECT article_author. article_idFROM article_authorWHERE author_idLIKE '$author')

于 2012-09-28T07:46:07.927 に答える
0

これはあなたの探求に役立つかもしれません:

$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

$keyword_mysql のアンダースコアとパーセントをバックスラッシュでエスケープすることもできます。

read_time 列が選択されたフィールドの一部でない場合、順序付けが機能するかどうかはわかりません。そうでない場合は、選択したフィールドに追加し、代わりに ORDER BY read_time を使用します。

于 2012-09-27T10:04:44.430 に答える