Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL选择字段“A”唯一的所有列_Sql_Jpa - Fatal编程技术网

SQL选择字段“A”唯一的所有列

SQL选择字段“A”唯一的所有列,sql,jpa,Sql,Jpa,我使用的是Java Spring JPA框架,我有下表: +----+---------+-----------+ | id | userID | creditID | +----+---------+-----------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 1 | 3 | | 4 | 2 | 4 | | 5 | 1 | 1

我使用的是Java Spring JPA框架,我有下表:

+----+---------+-----------+
| id | userID  | creditID  |
+----+---------+-----------+
|  1 | 1       | 1         |
|  2 | 1       | 2         |
|  3 | 1       | 3         |
|  4 | 2       | 4         |
|  5 | 1       | 1         |
|  6 | 1       | 2         |
+----+---------+-----------+
我想基于userID进行查询,以获取最新和唯一的creditid。我最近尝试了查询SELECT*FROM表,其中user_id='userId'GROUP BY credit_id,由于GROUP BY语句,我得到了运行时错误

实现以下结果的最简单语法是什么

+----+---------+-----------+
| id | userID  | creditID  |
+----+---------+-----------+
|  3 | 1       | 3         |
|  5 | 1       | 1         |
|  6 | 1       | 2         |
+----+---------+-----------+

你的问题的解决方案

  SELECT id, user_id, distinct(creditID) FROM Table WHERE user_id = 1

你的问题的解决方案

  SELECT id, user_id, distinct(creditID) FROM Table WHERE user_id = 1
在以下位置使用“选择并筛选”:

在以下位置使用“选择并筛选”:


我以前尝试过类似的方法,我想知道我使用的框架是否不支持DISTINCT。我得到以下错误:org.apache.derby.iapi.error.StandardException:语法错误:在第1行第12列遇到了distinct。请检查语法。我很惊讶它给了你任何错误。GROUPBY和distinct是非常常见的命令,因此它应该可以工作。我想问题出在列名或其他方面。我以前尝试过类似的方法,我想知道我使用的框架是否不支持DISTINCT。我得到以下错误:org.apache.derby.iapi.error.StandardException:语法错误:在第1行第12列遇到了distinct。请检查语法。我很惊讶它给了你任何错误。GROUPBY和distinct是非常常见的命令,因此它应该可以工作。我认为问题出在列名或其他方面。虽然此代码可以回答此问题,但提供了有关此代码为什么和/或如何回答此问题的附加上下文可以提高其长期价值。虽然此代码可以回答此问题,提供关于此代码为什么和/或如何回答此问题的附加上下文可提高其长期价值。
select t.*
from t
where t.id = (select max(t2.id) from t t2 where t2.userid = t.userid and t2.creditid = t.creditid) and
      t.userid = 1;