Im currently working on a live search and ran into the following problem:
The following query works but i would rather gather "topic" and "threadid" columns from table "bb1_threads" and only column "message" from table "bb1_posts"
SELECT posttopic AS topic, threadid FROM bb".$n."_posts WHERE message LIKE '%$search_string%' OR posttopic LIKE '%$search_string%' LIMIT 1
The following query also works but i cant search the field "message" from table bb1_posts this way:
SELECT topic, threadid FROM bb".$n."_threads WHERE topic LIKE '%$search_string%' LIMIT 1
So what i would like to have and tried to build for hours is a query that:
If someone could post a ready to use query, i would really appreciate that: Merry Christmas :)
I need this code is exactly what you need:
SELECT t.threadid, t.topic, p.message
FROM bb".$n."_threads t
LEFT JOIN bb".$n."_posts p ON t.threadid = p.threadid
WHERE t.topic LIKE '%$search_string%' OR p.message LIKE '%$search_string%'
ORDER BY (CASE
WHEN p.message LIKE '%$search_string%' THEN 1
ELSE 2
END) ASC
LIMIT 1
This query will select the threadid and topic columns from the bb1_threads table, and the message column from the bb1_posts table. It will search for the $search_string in both the topic and message columns, and then sort the results by which column the search string was found in first (so the result with a match in the message column will come before the result with a match in the topic column). Finally, it will return only the first result, using the LIMIT 1 clause.
This may work.
search bb1_posts
by message
and assign 1
as search_rank
search bb1_thread
by topic
and assign 2
as search_rank
union results from both, order by search_rank
and pick the first row
select threadid as threadid,
posttopic as topic,
1 as search_rank
from bb1_posts
where message like '%$search_string%'
union all
select threadid as threadid,
topic as topic,
2 as search_rank
from bb1_threads
where topic like '%$search_string%'
order by search_rank
limit 1;
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.