如何从sql表中提取值
> ID ProductID OptionName OptionValue SKU
>
> 1 709 Title Como test
> 2 709 Color Cobalt test
> 3 709 Color Crimson RNA.331.02.MM1K
> 4 709 Title Como G2 RNA.331.02.MM1K
> 7 709 Color another color test ipn
> 8 709 Title another title test ipn
从上表中,我想要以下内容
Select distinct OptionName from myTable where ProductID = 709 group by OptionValue
,但 sql server 在 group by 子句上给出错误,并且不知道如何将所有不同的值分组到不同的 OptionName?
或者我就是不能?
我想要的结果如下
[0] => array
[Color] => array
[0] => Cobalt test
[1] => Crimson
[2] => another color
[Title] => array
[0] => Como test
[1] => Como G2
[2] => another title
> ID ProductID OptionName OptionValue SKU
>
> 1 709 Title Como test
> 2 709 Color Cobalt test
> 3 709 Color Crimson RNA.331.02.MM1K
> 4 709 Title Como G2 RNA.331.02.MM1K
> 7 709 Color another color test ipn
> 8 709 Title another title test ipn
From the above table i want the following
Select distinct OptionName from myTable where ProductID = 709 group by OptionValue
but sql server is giving error on the group by clause and dont know how can i have all the different values grouped to distinct OptionName?
or i just can not ?
the result i want is as follows
[0] => array
[Color] => array
[0] => Cobalt test
[1] => Crimson
[2] => another color
[Title] => array
[0] => Como test
[1] => Como G2
[2] => another title
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您有 GROUP BY 子句时,您需要在您选择的字段(例如 FIRST()、LAST()、MAX()、AVG() ...)上使用聚合函数。另外,当你有 GROUP BY 时,distinct 不是必需的。您能否解释一下您想要提取哪些数据,而不仅仅是发布错误的查询?
编辑:
将产生以下结果:
转换为数组等是您必须在应用程序中完成的操作,而不是使用 SQL。
You need to use an aggregate function on the fields (like FIRST(), LAST(), MAX(), AVG() ...) you are selecting when you have a GROUP BY-clause. Also, distinct is not necessary when you have GROUP BY. Can you explain what data you want to extract and not only post a faulty query?
EDIT:
Will produce this:
Converting to arrays etc. is something you have to do in your application, not with SQL.
这是一种解决方案
,为什么 group by 有下划线
Here is one solution
why does group by have an underscore
尝试不使用 unique 并使用
OptionValue
使用 GROUP BY 将为您提供不同的组合。
Try it without the distinct and with the
OptionValue
in thereUsing the GROUP BY will give you the distinct combinations.
通过使用
GROUP BY OptionValue
,你的意思是...- 正常从我的查询中获取所有记录
- 将具有相同 OptionValue 的记录分组
- 为每个组仅返回一条记录
在您的情况下,您然后尝试返回 OptionName。这是一个问题,因为每个组有多个选项名称要显示,但每个组只有一条记录可以显示。
正如前面所说,正确的查询取决于您的需要,从您所写的内容来看,我并不是 100% 清楚这一点。 (给出您需要的结果的示例以及如何获得这些结果会有所帮助。)
我的猜测是您只需要两条记录(
Color
和Title
>)。如果是这样,您可以执行以下任一操作...SELECT DISTINCT OptionName FROM myTable WHERE ProductID = 709
SELECT OptionName FROM myTable WHERE ProductID = 709 GROUP BY OptionName
By using
GROUP BY OptionValue
you're saying this...- Get all the records from my query as normal
- Group records up that have the same OptionValue
- Return only one record for each of those groups
In your case, you then try to return the OptionName. This is a problem because there are multiple OptionName's to display per group, but only one record to do so per group.
As has been said, the right Query depends on what you need, and I'm not 100% clear on that from what you have written. (Giving an example of the results you need, and how to get those results, would help.)
My guess is that you just want two records (
Color
andTitle
). If so you can do either of these...SELECT DISTINCT OptionName FROM myTable WHERE ProductID = 709
SELECT OptionName FROM myTable WHERE ProductID = 709 GROUP BY OptionName