MySQL: - 为此查询添加where子句

时间:2016-08-11 13:53:39

标签: mysql sql

我有2个查询,但我很困惑在哪里添加where子句。

select supplier_id,count(*) c, party_name, SUM(po_value) po_value
from purchase_order join supplier s on s.id=supplier_id
group by supplier_id
having c>1 ORDER BY c DESC LIMIT 5

第二次查询

select warehouse_id, warehouse_name, SUM(available_quantity) available_quantity
from stock join warehouse s on s.id=warehouse_id 
group by warehouse_id 

在最后的两个查询中,我必须根据where org_id='152'

这样的ID进行提取

2 个答案:

答案 0 :(得分:2)

在GROUP BY之前添加它:

    SELECT supplier_id,count(*) c, party_name, SUM(po_value) po_value
    FROM purchase_order A JOIN supplier s ON s.id=supplier_id
    GROUP BY supplier_id
    WHERE A.org_id='152'
    HAVING c>1 ORDER BY c DESC LIMIT 5

    SELECT warehouse_id, warehouse_name, SUM(available_quantity) available_quantity
    FROM stock G JOIN warehouse s ON s.id=warehouse_id 
    WHERE G.org_id='152'
    GROUP BY warehouse_id 

另外,从可读性的角度来看,我建议使用大写标记标准SQL术语:)

本页的第一个例子也回答了你提出的问题:

Modifier.splitBlock

编辑:已更新以消除where子句的歧义。

答案 1 :(得分:1)

请参阅下面的编辑查询:

class Program
{
    static void Main()
    {

        var table1 = new List<Dictionary<string, string>>{
        new Dictionary<string,string>{
        {"md5","0x1234"},
        {"file_name","name1234"},
        {"size","1234"}},

        new Dictionary<string,string>{
        {"md5","0x1234"},
        {"file_name","name1234"},
        {"size","1234"}},
        };

        var table2 = new List<Dictionary<string, string>>{
        new Dictionary<string,string>{
        {"md5","0x1234"},
        {"file_name","name1234"},
        {"size","1234"}},

        new Dictionary<string,string>{
        {"md5","0x5678"},
        {"file_name","name5678"},
        {"size","5678"}}
        };

        var diff = table2.Except(table1, new DictionaryComparer());

        foreach (var item in diff)
        {
            foreach (var value in item)
            {
                Console.WriteLine(value);
            }
            Console.WriteLine("+++++++++++++++++++++");
        }

        Console.ReadKey();
    }

    public class DictionaryComparer : IEqualityComparer<Dictionary<string, string>>
    {
        public bool Equals(Dictionary<string, string> first, Dictionary<string, string> second)
        {
            if (first == second) return true;
            if ((first == null) || (second == null)) return false;
            if (first.Count != second.Count) return false;

            foreach (var k in first.Keys)
                if (!second.ContainsKey(k))
                    return false;

            foreach (var k in first.Keys)
                if (!first[k].Equals(second[k]))
                    return false;

            return true;
        }

        public int GetHashCode(Dictionary<string, string> obj)
        {
            int resultValue = 0;

            if (obj == null)
            {
                return 0;
            }        

            foreach (var item in obj)
            {
                resultValue += (item.Key.Length + item.Value.Length);
            }

            return resultValue;
        }

    }

如果您想在另一个表格中查找,则会与select supplier_id,count(*) c, party_name, SUM(po_value) po_value from purchase_order join supplier s on s.id=supplier_id WHERE purchase_order.org_id='152 group by supplier_id having c>1 ORDER BY c DESC LIMIT 5 使用s.org_id中的org_id进行比较。

第二个

purchase_order

(请注意,空格是可折叠的)。有关完整详细信息,请参阅选择的手册部分:http://dev.mysql.com/doc/refman/5.7/en/select.html