1

I need to get the "title" field if the "content" field contains a certain phrase. With the title I can do the actual query to get the posts.

-- psuedo code --

$query1 ='SELECT `title` FROM `posts` WHERE `content` CONTAINS 'matchtest' 
ORDER BY `id` ASC, LIMIT 1';
$titlefromquery1;

$query2 = 'SELECT * FROM `posts` WHERE `title` = 'Reply to: ' . $titlefromquery1;
$repliesfromquery2;

The replies don't have the phrase to match fortunately the "titles" will always be "Reply to: $title" which is why both queries are needed it seems

Is there any way conditionally or otherwise to avoid a double query here?

2
  • 2
    pseudo code: self-join Commented Aug 26, 2015 at 4:55
  • I think, there is no other way. But you can change your system to INT. So that: You have posts so you can group replies and mains by parent_id parameter. with that you can group parent_id's and fetch with one query Commented Aug 26, 2015 at 4:55

2 Answers 2

1
SELECT * FROM posts 
WHERE title= 
concat_ws('Reply to: ',
(SELECT title FROM posts WHERE <put_your_condition> ORDER BY id ASC LIMIT 1))
Sign up to request clarification or add additional context in comments.

Comments

1

You can use temporary variables to achieve this:

SELECT @title:=`title` FROM `posts` WHERE `content` CONTAINS 'matchtest' ORDER BY `id` ASC, LIMIT 1;

SELECT * FROM `posts` WHERE `title` = CONCAT('Reply to: ' , @title);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.