如何在mysql的条件下比较表与自身

时间:2013-09-26 18:53:38

标签: mysql

嗨我需要一些帮助我有一个表销售如下..我想比较它自己并显示一个结果公司应该不等于公司和价格应该等于价格..我尝试了很多查询在mysql但没有得到理想的结果请帮助我做到这一点!告诉正确的查询 非常感谢你们!

sells(company,drink,price)

我试过试试

mysql>'select company,drink,price from sells JOIN s ON(price=price1) where (comp
any1!=company);'

而s是销售的副本表

+----------------+----------------+-------+
| company        | drink          | price |
+----------------+----------------+-------+
| PepsiCo        | Mirinda        |    45 |
| PepsiCo        | Pepsi          |    45 |
| Coca-Cola      | Coca-cola zero |    45 |
| Nestle         | Nescafe        |    35 |
| Nestle         | Nescafe        |    35 |
| Nestle         | Nescafe        |    35 |
| Nestle         | Nescafe        |    35 |
| Nestle         | Nescafe        |    35 |
| Nestle         | Nescafe        |    35 |
| Coca-Cola      | Coca-cola zero |    45 |
| Shezan         | Twister        |    25 |
| Nestle         | Fruita vitals  |    25 |
| Murree Brewery | Big Apple      |    35 |
| Murree Brewery | Big Lemon      |    35 |
| Murree Brewery | Big Orange     |    35 |
| Murree Brewery | Peach Malt     |    35 |
| Murree Brewery | Lemon Malt     |    35 |
| Murree Brewery | Apple Malt     |    35 |
+----------------+----------------+-------+

1 个答案:

答案 0 :(得分:-1)

试试这个?

SELECT T1.company, T2.company, T1.price 
FROM sells T1 
JOIN sells T2
WHERE 
T1.company != T2.company
AND
T1.price = T2.price
GROUP BY T1.company, T1.price;