如何从sql表中提取值

发布于 2024-11-08 16:05:01 字数 978 浏览 0 评论 0原文

> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最佳男配角 2024-11-15 16:05:01

当您有 GROUP BY 子句时,您需要在您选择的字段(例如 FIRST()、LAST()、MAX()、AVG() ...)上使用聚合函数。另外,当你有 GROUP BY 时,distinct 不是必需的。您能否解释一下您想要提取哪些数据,而不仅仅是发布错误的查询?

编辑

SELECT OptionName, OptionValue FROM myTable WHERE ProductID = 709 ORDER BY OptionName ASC

将产生以下结果:

> OptionName    OptionValue             
> 
> Color          Cobalt test
> Color          Crimson                
> Color          another color   
> Title          Como   test    
> Title          Como G2
> Title          another title

转换为数组等是您必须在应用程序中完成的操作,而不是使用 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:

SELECT OptionName, OptionValue FROM myTable WHERE ProductID = 709 ORDER BY OptionName ASC

Will produce this:

> OptionName    OptionValue             
> 
> Color          Cobalt test
> Color          Crimson                
> Color          another color   
> Title          Como   test    
> Title          Como G2
> Title          another title

Converting to arrays etc. is something you have to do in your application, not with SQL.

风吹雨成花 2024-11-15 16:05:01

这是一种解决方案

Select OptionName from myTable where ProductID = 709 limit 1

,为什么 group by 有下划线

Here is one solution

Select OptionName from myTable where ProductID = 709 limit 1

why does group by have an underscore

谈场末日恋爱 2024-11-15 16:05:01

尝试不使用 unique 并使用 OptionValue

select OptionName
from myTable 
where ProductID = 709 
group by OptionName, OptionValue

使用 GROUP BY 将为您提供不同的组合。

Try it without the distinct and with the OptionValue in there

select OptionName
from myTable 
where ProductID = 709 
group by OptionName, OptionValue

Using the GROUP BY will give you the distinct combinations.

沫尐诺 2024-11-15 16:05:01

通过使用GROUP BY OptionValue,你的意思是...
- 正常从我的查询中获取所有记录
- 将具有相同 OptionValue 的记录分组
- 为每个组仅返回一条记录

在您的情况下,您然后尝试返回 OptionName。这是一个问题,因为每个组有多个选项名称要显示,但每个组只有一条记录可以显示。

正如前面所说,正确的查询取决于您的需要,从您所写的内容来看,我并不是 100% 清楚这一点。 (给出您需要的结果的示例以及如何获得这些结果会有所帮助。)

我的猜测是您只需要两条记录(ColorTitle >)。如果是这样,您可以执行以下任一操作...

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 and Title). 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文