简体   繁体   English

如何从sql表中提取值

[英]how to extract values from sql table

> 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? 但是SQL Server在group by子句上给出错误,并且不知道如何将所有不同的值分组到不同的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

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. 当您具有GROUP BY子句时,您需要在要选择的字段(例如FIRST(),LAST(),MAX(),AVG()...)上使用聚合函数。 Also, distinct is not necessary when you have GROUP BY. 另外,当您具有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. 转换为数组等是您必须在应用程序中完成的工作,而不是使用SQL。

Here is one solution 这是一个解决方案

Select OptionName from myTable where ProductID = 709 limit 1

why does group by have an underscore 为什么group by下划线

Try it without the distinct and with the OptionValue in there 尝试不带区别的选项并在其中使用OptionValue

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

Using the GROUP BY will give you the distinct combinations. 使用GROUP BY将给您不同的组合。

By using GROUP BY OptionValue you're saying this... 通过使用GROUP BY OptionValue您说的是...
- Get all the records from my query as normal -像往常一样从我的查询中获取所有记录
- Group records up that have the same OptionValue -组记录具有相同的OptionValue
- Return only one record for each of those groups -每个组仅返回一条记录

In your case, you then try to return the OptionName. 对于您的情况,然后尝试返回OptionName。 This is a problem because there are multiple OptionName's to display per group, but only one record to do so per group. 这是一个问题,因为每个组要显示多个OptionName,但是每个组只能显示一个记录。


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. 就像已经说过的那样,正确的查询取决于您的需求,而我对您所写内容的了解还不是100%清楚。 (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 ). 我的猜测是,您只需要两个记录( ColorTitle )。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM