mysql返回表的一列,MySQL 5.7根据不同的列返回表的所有列

I just upgraded to MySQL 5.7 and unfortunately for me, some of the functionality of GROUP BY is gone. I wanted to select all movies from my movies table with as long as the movies.id of type int is not a duplicate. My previous query in MySQL 5.6 was:

SELECT *

FROM movies

WHERE movies.title LIKE '%example%'

GROUP BY movies.id

If I had two movies with the same id, it would only display one movie, instead of that movie and its duplicates.

When I upgraded to MySQL 5.7, the GROUP BY gave me errors and I was instead told to use ORDER BY. However this query:

SELECT *

FROM movies

WHERE movies.title LIKE '%example%'

ORDER BY movies.id

Does return duplicate movies. So, is there a way to filter this out, and only return a row if it isn't a duplicate?

Edit: For example if this is my movies table:

movies

==================

| id | title |

==================

| 1 | example |

------------------

| 2 | example |

------------------

| 1 | example |

------------------

Here is the output of each query:

Previous query result (with MySQL 5.6)

=======

1 | example

2 | example

New query result (with MySQL 5.7 and ORDER BY)

=======

1 | example

1 | example

2 | example

I want the final result to contain no duplicates (so the result should look like the first query result).

Edit 2: I understand I was sort of abusing the way MySQL handled GROUP BY. Unfortunately, I do not have much experience with MySQL and got that answer from StackOverflow. I would just like to return all columns in my table that do not contain duplicate ids.

解决方案

I believe would be easy to use distinct keyword

SELECT distinct movies.*

FROM movies

WHERE movies.title = 'example'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值