0

how would one achieve the ability to select multiple columns that are similar in the statement below? I'm using the DISTINCT MySQL for the body, which works fine... but I cannot obtain additional information (columns) that goes with it. Any ideas would be appreciated, please let me know if you need more information from me.

SELECT DISTINCT T.body
FROM Posts T
   LEFT JOIN Posts T2 ON T.body != T2.body
WHERE T.body LIKE CONCAT('%', T2.body, '%')
UNION 
SELECT DISTINCT T2.body
FROM Posts T
   LEFT JOIN Posts T2 ON T.body != T2.body
WHERE T.body LIKE CONCAT('%', T2.body, '%')
ORDER BY body;

1 Answer 1

1

Try adding column that you want to select. for example

SELECT DISTINCT T.body, T.Column1, T.Column2 FROM Posts T LEFT JOIN Posts T2 ON T.body != T2.body WHERE T.body LIKE CONCAT('%', T2.body, '%') UNION SELECT DISTINCT T2.body FROM Posts T LEFT JOIN Posts T2 ON T.body != T2.body WHERE T.body LIKE CONCAT('%', T2.body, '%') ORDER BY body;"

Sign up to request clarification or add additional context in comments.

3 Comments

I receive the following error with that statement, although it's close: #1222 - The used SELECT statements have a different number of columns ; What should I do to correct this?
SELECT DISTINCT T.body, T.IDPosts, T.latitude, T.longitude, T.post_date FROM Posts T LEFT JOIN Posts T2 ON T.body != T2.body WHERE T.body LIKE CONCAT('%', T2.body, '%') UNION SELECT DISTINCT T2.body FROM Posts T LEFT JOIN Posts T2 ON T.body != T2.body WHERE T.body LIKE CONCAT('%', T2.body, '%') ORDER BY body;
Try make the same number of columns on your first select to the your second select. I mean the columns that you select mus be equal.

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.