0

Can I, and if so, how do I prevent mysql from returning duplicate results

 SELECT meta_id, post_id, DISTINCT meta_value FROM wp_postmeta WHERE post_id='69' AND meta_value LIKE '%iframe%'

I want to get just the one row, so ignore any further items if meta_value duplicated!

Putting DISTINCT where it is, returns no results, but query works as expected if removed, so it is obviously wrong to put it there!

1
  • 1
    SELECT meta_id, post_id,meta_value FROM wp_postmeta WHERE post_id='69' AND meta_value LIKE '%iframe%' GROUP BY meta_value Commented Nov 30, 2017 at 10:07

1 Answer 1

1

If you want only one result you can use LIMIT in MySql like

SELECT meta_id, post_id, DISTINCT meta_value 
FROM wp_postmeta 
WHERE post_id='69' 
AND meta_value LIKE '%iframe%'
LIMIT 1

OR in Sql you can use

SELECT TOP 1 meta_id, post_id, DISTINCT meta_value 
FROM wp_postmeta 
WHERE post_id='69' 
AND meta_value LIKE '%iframe%'
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea, but I need it to return a unique set, meta_value seems to have duplicates for some reason!
Then I think you can go with the GROUP BY meta_value as suggested by @laiju in the previous comment.

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.