[英]Comparison of items in same SQL table
I have a table in my database which looks like: 我的数据库中有一个表,看起来像:
+-------------+---------+--------+
| ProductName | Status | Branch |
+-------------+---------+--------+
| P1 | dead | 1 |
| P1 | dead | 2 |
| P2 | expired | 1 |
+-------------+---------+--------+
I want to show the result after comparison in my java app as: 我想在我的Java应用程序中比较后显示结果:
+-------------+---------+--------+
| ProductName | Branch 1|Branch 2|
+-------------+---------+--------+
| P1 | dead | dead|
| P2 | expired | |
+-------------+---------+--------+
i have been thinking about it but couldn't sort out any solution. 我一直在考虑,但无法解决任何解决方案。 Any suggestions about how can i do it? 关于我该怎么办的任何建议?
You can achieve it by this way in mysql. 您可以通过这种方式在mysql中实现。 SQLFiddle Demo SQLFiddle演示
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'GROUP_CONCAT(case when branch = ''',
branch,
''' then status ELSE NULL end) AS ',
CONCAT('Branch',branch)
)
) INTO @sql
FROM Table1;
SET @sql = CONCAT('SELECT productName, ', @sql, '
FROM Table1
GROUP BY productName');
PREPARE stmt FROM @sql;
EXECUTE stmt;
But I would personally suggest you to achieve same thing using Java Collection
by just executing the simple select mysql query. 但是我个人建议您通过执行简单的select mysql查询来使用Java Collection
实现相同的目的。
Update : Please try below given code snap. 更新 :请尝试以下给出的代码捕捉。
package com.plugin.jira.api.util;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Product {
private String productName;
private String status;
private Integer branch;
private Map<Integer, String> branchStatusMap = new HashMap<>();
/**
* @return the productName
*/
public String getProductName() {
return productName;
}
/**
* @param productName the productName to set
*/
public void setProductName(String productName) {
this.productName = productName;
}
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
this.status = status;
if (this.branch != null) {
branchStatusMap.put(this.branch, this.status);
}
}
/**
* @return the branchStatusMap
*/
public Map<Integer, String> getBranchStatusMap() {
return branchStatusMap;
}
/**
* @return the branch
*/
public Integer getBranch() {
return branch;
}
/**
* @param branch the branch to set
*/
public void setBranch(Integer branch) {
this.branch = branch;
if (this.status != null) {
branchStatusMap.put(this.branch, this.status);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((productName == null) ? 0 : productName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (productName == null) {
if (other.productName != null)
return false;
} else if (!productName.equals(other.productName))
return false;
/** make sure that the hasCode is also same for safer side. */
if (this.hashCode() == other.hashCode()) {
other.branchStatusMap.put(this.branch, this.status);
}
return true;
}
public static void main(String[] args) {
Set<Product> products = new HashSet<>();
Product p;
p = new Product();
p.setProductName("P1");
p.setStatus("dead");
p.setBranch(1);
products.add(p);
p = new Product();
p.setProductName("P1");
p.setStatus("dead");
p.setBranch(2);
products.add(p);
p = new Product();
p.setProductName("P2");
p.setStatus("expired");
p.setBranch(1);
products.add(p);
/** now you will get two results in set with map of branches */
for (Product product : products) {
System.out.println("Product : " + product.getProductName());
for (Map.Entry<Integer, String> entry : product.getBranchStatusMap().entrySet()) {
System.out.println("Branch " + entry.getKey() + " : " + entry.getValue());
}
System.out.println("----------------------------------");
}
}
}
You can use a "pivot sentence" like this: 您可以像这样使用“枢轴句子”:
SELECT ProductName,
MAX(CASE
WHEN Branch=1
THEN Status
ELSE NULL
END) AS 'Branch 1',
MAX(CASE
WHEN Branch=2
THEN Status
ELSE NULL
END) AS 'Branch 2'
FROM MyTable
GROUP BY ProductName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.