| 2:36 pm on Nov 20, 2012 (gmt 0)|
welcome to WebmasterWorld, yugonomex!
you either have to use an OR in your WHERE clause and do the sort in PHP,
or make 3 queries in order, using an AND clause in your WHERE clause for the first query.
| 5:26 pm on Nov 20, 2012 (gmt 0)|
You can also use a union to let mysql combine the 3 selects into one result set.
| 5:31 pm on Nov 20, 2012 (gmt 0)|
i'm not sure how you could ORDER BY (both and then one and then the other) as specified using a UNION.
| 5:42 pm on Nov 20, 2012 (gmt 0)|
In pseudo code:
( select * from whatever where "Narberth" and "Pembrokeshire" order by whatever limit howmany)
( select * from whatever where "narberth" and not "Pembrokeshire" order by whatever limit howmany)
( select * from whatever where "pembrokshire" and not "narberth" order by whatever limit howmany)
AFAIK the order the results come in are the same as the order of the union - although I admit this does not seem to be documented as such.
You can order and limit the individual results and the complete set by using parentheses.
Note that I'm myself avoiding duplicates so union all can be used and that might be the trick to keep the order. (Not tested it)
| 5:50 pm on Nov 20, 2012 (gmt 0)|
i had never assumed the order would be maintained nor had i tested it.
let us know how it works, yugonomex.
| 6:08 pm on Nov 20, 2012 (gmt 0)|
Regardless you could always include an additional column you add the select it came from and then sort on that, SQL is a fully featured language. Most you can imagine to do can be done in one (complex) query.
Actually the docs state the approach by adding a column to indicate what select the row comes from
To cause rows in a UNION result to consist of the sets of rows retrieved by each SELECT one after the other, select an additional column in each SELECT to use as a sort column and add an ORDER BY following the last SELECT:
(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
Quoted from [dev.mysql.com...]
| 11:51 pm on Nov 20, 2012 (gmt 0)|
good one - i often forget that trick!
| 2:47 am on Nov 21, 2012 (gmt 0)|
You should be able to include a sorting column something like:
select .... rest of the query here, for both keywords ... ORDER BY (**kw-query1**)+(**kw-query2**) DESC
Or; use MATCH AGAINST as that automatically does this kind of scoring for you.
| 2:03 pm on Nov 22, 2012 (gmt 0)|
tbl_index t inner join
count(*) as rank
r.keyword_id in ( $keywords )
on r2.file_id = t.file_id
Hey guys thanks again for the info provided, the solution in the end is shown above. I used ranks to place the most appropriate rows at the top of the table. All the help was really appreciated and you'll most likely be hearing from me again soon haha :)