Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distinct did not take effect when shared with count #5821

Closed
ssmmtt opened this issue Oct 28, 2022 · 5 comments
Closed

distinct did not take effect when shared with count #5821

ssmmtt opened this issue Oct 28, 2022 · 5 comments
Assignees
Labels
type:question general questions

Comments

@ssmmtt
Copy link

ssmmtt commented Oct 28, 2022

Your Question

distinct did not take effect when shared with count, mysqldb.

db.Model(&User{}).Distinct("name", "age").Count(&count)

It does not take effect.

The actual sql statement of is "SELECT COUNT(*) FROM users"




## The document you expected this should be explained
https://gorm.io/docs/advanced_query.html#count


@ssmmtt ssmmtt added the type:question general questions label Oct 28, 2022
@ssmmtt ssmmtt closed this as completed Oct 28, 2022
@itaranto
Copy link

itaranto commented Jul 28, 2023

@ssmmtt Can you explain why did you close this? Where you able to fix the issue? How?

@ssmmtt
Copy link
Author

ssmmtt commented Jul 28, 2023

@ssmmtt Can you explain why did you close this? Where you able to fix the issue? How?

@itaranto
MySQL does not allow this, it is not related to Gorm

@ssmmtt ssmmtt reopened this Jul 28, 2023
@ssmmtt ssmmtt closed this as completed Jul 28, 2023
@itaranto
Copy link

itaranto commented Jul 28, 2023

@ssmmtt Can you explain why did you close this? Where you able to fix the issue? How?

@itaranto MySQL does not allow this, it is not related to Gorm

I don't know about MySQL, but in PostgreSQL it's perfectly valid to do:

SELECT COUNT(DISTINCT(name, age)) FROM users

GORM's docs for Distinct say:

// Distinct specify distinct fields that you want querying
//
//	// Select distinct names of users
//	db.Distinct("name").Find(&results)
//	// Select distinct name/age pairs from users
//	db.Distinct("name", "age").Find(&results)

But the behavior of db.Distinct("name", "age").Count(&results) seems to be undocumented.

Like you mentioned above, GORM generates incorrect SQL for this:

SELECT COUNT(*) FROM users

I think this issue shouldn't be closed.

@ssmmtt
Copy link
Author

ssmmtt commented Aug 1, 2023

@itaranto
image

The error "Operand should contain 1 column(s)" indicates that the operand in the SQL query should contain only 1 column, but you have provided multiple columns instead.
This usually happens when some functions or operations in the SQL statement require only one column as a parameter, but multiple columns were passed in instead.
Some common cases include:

  • Using aggregate functions like SUM(), AVG(), but passing in multiple columns instead of just one.
  • Doing DISTINCT on multiple columns instead of just one column.
  • Using scalar functions like UPPER(), LEN() in SELECT, but supplied with multiple columns.
  • Grouping by multiple columns in GROUP BY clause instead of just one column.
  • JOIN condition contains multiple columns, but it should compare just two columns.
    To resolve this, you need to check the SQL statement and find where multiple columns were passed in as a single column parameter, and correct it to use only one column.
    Some examples:
  • SUM(col1, col2) should be changed to SUM(col1)
  • SELECT DISTINCT col1, col2 should be SELECT DISTINCT col1
  • UPPER(col1, col2) should be UPPER(col1)
  • GROUP BY col1, col2 should be GROUP BY col1
  • ...ON t1.col1 = t2.col1 AND t2.col2 should be ...ON t1.col1 = t2.col1
    So in summary, check and pass the correct number of columns as parameters to avoid the "Operand should contain 1 column(s)" error.

@bingtianyiyan
Copy link

1694509410115 if i have one select column for use count,it will generate sql like: example sql --> SELECT COUNT(`Id AS id`) FROM `t_test` ; in mysql execute fail 1694509537940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

4 participants