Non duplicates mysql query php mysql - php

hey, i`m just wondering what I should do to this query to have it not return any duplicate items.
SELECT tag FROM tags WHERE MATCH (tag)
AGAINST ('$sql_items' IN BOOLEAN MODE) LIMIT 5
Ive tried a bunch of different stuff but I cant seem to get it to work :( I`m using php

DISTINCT is your friend :) (OR GROUP BY)
SELECT DISTINCT tag FROM tags WHERE MATCH (tag)
AGAINST ('$sql_items' IN BOOLEAN MODE) LIMIT 5

What about using a distinct in your select clause ?
A bit like this, I'd say :
SELECT distinct tag
FROM tags
WHERE MATCH (tag)
AGAINST ('$sql_items' IN BOOLEAN MODE)
LIMIT 5

Consider
SELECT tag
FROM tags
WHERE MATCH (tag) AGAINST ('$sql_items' IN BOOLEAN MODE)
GROUP BY tag
LIMIT 5
This assumes you may need to select other fields from tags. If you just need tag, then DISTINCT tag may be all it takes.

Related

Can you combine a simple MYSQL Statement with a FullText Statement

I've built an internal DB / Search Engine for art creatives. I'm trying to create a search criterion where you can query one column in the database and also search several columns in the database for a phrase search using FullText Search. The example of a search query might be: November {and} Black Friday. November would search for creatives matching the created_for column and the black friday would search headline, subheadline and additional_text columns with a fulltext search. Any ideas of how to accomplish this would be really helpful!
SELECT
(SELECT * FROM headlines WHERE created_for = '$searchString' AND image_slug <> '')
(SELECT *, MATCH(headline) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiScore, MATCH(subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiSecondScore
FROM `headlines`
WHERE MATCH(headline, subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE))
I've tried adding a UNION statement before the second Select statement, but I get an error message saying the columns don't match. Not sure what I've got wrong here, but thanks in advance for your help!
Use AND in the WHERE clause.
SELECT *, MATCH(headline) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiScore, MATCH(subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiSecondScore
FROM headlines
WHERE created_for = '$searchString'
AND image_slug <> ''
AND MATCH(headline, subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE)
UNION would get results that match either of the criteria, not both of them. And when you use UNION, both subqueries have to return the same number of columns -- you would have to add extra columns to the first query to match the MultiScore and MultiSecondScore columns of the first query.

best-result search using multiple tags on a column that stores values as CSV

I have tags stored in a column that are separated by commas. I'm trying to create a best match search.
I do not want to match partial strings, so I've added a comma at the beginning and end of each string like so ,apple,orange,banana, so that I can look up using LIKE '%,apple,% and it doesn't care what the position is.
how do I get the list of ids in order based on the best match compared to the search tags
is there a pure mySQL way of doing this?
Right now I'm dealing with it in PHP by looping through each tag and building out an array that counts the number of matches. 99% of time there will be a max of up to 5 tags being searched.
also in my case I'm using two tag fields/columns that have separate tag categories
SQL FIDDLE DEMO
Without a schema I imagine you need something like this.
SELECT
t.tagID,
t.tagText,
num_match.num_appear,
((LENGTH(`tagText`) - LENGTH(REPLACE(`tagText`, ',', '')))/LENGTH(',')) - 1 as wordnumber,
(num_match.num_appear / (((LENGTH(`tagText`) - LENGTH(REPLACE(`tagText`, ',', '')))*1.0/LENGTH(',')) - 1)) *100 as porcentage
FROM
tblTAG t left join
(SELECT tagID, count(tagID) as num_appear
FROM
(( SELECT tagID
FROM tblTAG
WHERE tagText LIKE '%,apple,%'
) union all
( SELECT tagID
FROM tblTAG
WHERE tagText LIKE '%,banana,%'
)) as result
GROUP BY tagID
) as num_match
ON t.tagID = num_match.tagID

MATCH (somefeald) AGAINST ('+".$_GET['search']."' IN BOOLEAN MODE) not work

Why does this SQL query return a blank result?
select
webs.title,webs.disc,webs.logo,webs.id
from
webs
JOIN rel ON rel.webid=webs.id
JOIN catagory on catagory.id=rel.catid
where
webs.app!=0
AND MATCH (webs.title,webs.city,webs.state,webs.url,catagory.catname) AGAINST ('+".$_GET['search']."' IN BOOLEAN MODE)
This is a query for multiples searches in multiple tables and fields.
I also want to order the result by the best matching word.
If you want to use "MATCH() ... AGAINST", first create fulltext index on the field which you want in match.

Optimizing auto-complete FULLTEXT SQL query

I have the following query which is used in order to do an auto-complete of a search box:
SELECT *, MATCH (screen_name, name) AGAINST ('+query*' IN BOOLEAN MODE) AS SCORE
FROM users
WHERE MATCH (screen_name, name) AGAINST ('+query*' IN BOOLEAN MODE)
ORDER BY SCORE DESC LIMIT 3
I also have a FULL TEXT index on screen_name & name (together). When this table was relatively small (50k) this worked great. Now the table is ~200k and it takes seconds(!) to complete each query. I'm using MySql MyISAM. Is this reasonable? What directions might I check in order to improve this as surely it doesn't satisfy the needs of an auto-complete query.
MYSQL Match against is really slow, you should look into alternatives like Sphinx Search Server.

mysql multi-table fulltext query problem?

I am making a multi-table fulltext query.But I met some question.
I need make a query like
(SELECT
title,content,date,cat
FROM article1
WHERE
cat='Science fiction'
AND
MATCH (title,content)
AGAINST
('+Harry +Potter' IN BOOLEAN MODE))
UNION
(SELECT
title,content,date
FROM article3
WHERE MATCH (title,content)
AGAINST
('+Harry +Potter' IN BOOLEAN MODE))
Order By date DESC LIMIT 10
But it caused Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
If I deleted cat='Science fiction' AND it can pass the test.
Where is the problem? If I want make a query what I want: first fulltext query require need meet cat='Science fiction'. Thanks a lot.
You are trying to UNION together result sets that return a different number of columns. Your first query returns 4 columns (title,content,date,cat) while your second only returns 3 (title,content,date). Each query must return the same number of columns.
I don't know why removing the cat LIKE 'Science fiction' makes the query work. As written, it should not work in either case. UNION requires both parts of the queries to produce the same number of columns with compatible types. Your UNION has 4 columns in the first part and 3 columns in the second:
SELECT title, content, date, cat FROM article1
UNION
SELECT title, content, date FROM article3
Did mysql_query return FALSE, indicating a problem parsing your query? If you blindly passed "FALSE" into mysql_fetch_array(), I'd expect to see that kind of error.
try
cat LIKE 'Science fiction'

Categories