1

I am having a problem using distinct with *. I have a table that as a join and I am trying to do a statement like:

SELECT DISTINCT Name, * FROM table_a JOIN table_a.id=table_b.id WHERE status=1

But it is not allowing me to do so. Is there a way of using Distinct with * option?

5
  • 1
    Which SQL dialect are we talking about? Also, are you sure the DISTINCT is the problem and not the Name, * part? Commented Nov 21, 2010 at 14:46
  • Why is this tagged both mysql and postgresql? Commented Nov 21, 2010 at 14:48
  • I am doing this for both mysql and postgresql. I have the tables joined on id and it is the only duplicated field yet is throws an error Commented Nov 21, 2010 at 15:21
  • The error message is ; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM Commented Nov 21, 2010 at 15:22
  • DISTINCT * does not make any sense. That will include the primary key column(s) and so by defintion all rows will be returned Commented Apr 22, 2012 at 14:39

4 Answers 4

1

I don't have access to test this right now, but I suspect that the problem is not with distinct but rather that you have columns in each table with the same names (like id) and it doesn't know which of the two conflicting columns to choose. Does it change if you do select distinct table_a.*, table_b.* ...?

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

2 Comments

Using select distinct table_a.*, table_b.* it no longer produces an error but when I do that, distinct also does not work
@Devin -- then choose the columns that you want to choose distinctly and list them instead of using *.
1

DISTINCT * probably will return all rows, because they are distinct:-)

To get all different names use DISTINCT name or to retrieve some statistics about names (e.g. count) use SELECT name, COUNT(*) FROM ... GROUP BY name.

Comments

0

It is not clear what you want to do.

Maybe you want to return only one column with a given name (not two id columns).

In this case the standard SQL query is like this:

SELECT * FROM table_a JOIN table_b USING (id) WHERE status=1

The keyword DISTINCT means that after performing the select, duplicate rows are eliminated (not duplicate column names).

Comments

0

Basically the solution is more like this:

SELECT *
FROM table_a, table_b
JOIN table_a.id = table_b.id
WHERE status = 1
GROUP BY table_a.name

This is useful, if you want to avoid the enumeration of attributes. When you do, and the relation changes (one attribute is removed or added), your query will not work.

Read this: MySQL 8.3.1.13. DISTINCT Optimization

3 Comments

This is a very old question! Your syntax is not valid (hint: shouldn't your FROM clause include a table named table_b?) I suspect the answer was SELECT * FROM table_a NATURAL JOIN table_b WHERE status = 1;
@onedaywhen: Sorry, I've just revised the missing lexical. What do you mean on the answer? The accepted answer is not working, and it does not solve the problem with DISTINCT when you use *.
Your revised query returns duplicate columns. I can only guess, of course, but I think the OP wanted distinct columns (rather than distinct rows) and NATURAL JOIN is a good way to achieve that.

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.