如何选择mysql结果?

时间:2017-12-01 04:37:48

标签: php mysql

发票

  public class Solution {
        public IList<IList<string>> SolveNQueens(int n) 
        {
            IEnumerable<PartialQueens> sub = new List<PartialQueens>(){
                new PartialQueens(n)};

            for(int i=0;i<n;i++)
            {
                sub = sub.SelectMany(x => x.Next(i)).ToList(); 
            }

           return sub.Select(x => x.ToPosition()).ToList();
        }
    }

   public class PartialQueens
    {
    public byte FREE   = 0;
    public byte BLOCKED = 1;
    public byte QUEEN    = 2; 

    public byte[,] fill;
    int n;

    public PartialQueens(int n)
    {
        this.n = n;
        fill = new byte[n,n];
    }

    public PartialQueens(byte[,] fill, int n)
    {
        this.fill = fill;
        this.n    = n;
    }

    public PartialQueens Fill(int row, int column)
    {
        byte[,] newFill = fill.Clone() as byte[,];

        newFill[row,column] = QUEEN;

        Action<int,int> f = (x,y) => 
        {
            if(y >= 0 && y < n)
                newFill[x,y] = BLOCKED;
        };

        for(int i=1;i<n-row;i++)
        {
            f(row+i,column+i);
            f(row+i,column-i);
            f(row+i,column);
        }

        return new PartialQueens(newFill,n);
    }

    public IEnumerable<PartialQueens> Next(int row)
    { 
        for(int j=0;j<n;j++)
        {            
            if(fill[row,j] == FREE)
                yield return Fill(row,j);
        }
    }  

    public IList<string> ToPosition()
    {
        return Enumerable.Range(0,n).Select(i => ConvertRow(i)).ToList();
    }

    public string ConvertRow(int i)
    {
        StringBuilder builder = new StringBuilder();

        for(int j=0;j<n;j++)
        {
            if(fill[i,j] == QUEEN)
                builder.Append("Q");
            else
                builder.Append(".");
        }

        return builder.ToString();
    }
}

我想选择除重复BRN之外的所有行。 (如果BRN中有两个/更多的ge,那么它应该只选择一个)

我试过了:

+----+-----+---------+-------+
| Sr | BRN |  Name   |  Amnt |
+----+-----+---------+-------+
| 1  | 1   | John    |  10   |
| 2  | 1   | John    |   4   |
| 3  | 2   | Belly   |   4   |
| 4  | 3   | John    |  14   |
| 5  | 4   | John    |   5   |
| 6  | 4   | John    |  14   |
+----+-----+---------+-------+

预期结果:

SELECT *(DISTINCT BRN) FROM invoice

3 个答案:

答案 0 :(得分:0)

SELECT * FROM invoice WHERE Date&gt; =:fdate GROUP BY BRN

答案 1 :(得分:0)

鉴于下表:

+----+-----+---------+-------+
| Sr | BRN |  Name   |  Amnt |
+----+-----+---------+-------+
| 1  | 1   | John    |  10   |
| 2  | 1   | John    |   4   |
| 3  | 2   | Belly   |   4   |
| 4  | 3   | John    |  14   |
| 5  | 4   | John    |   5   |
| 6  | 4   | John    |  14   |
+----+-----+---------+-------+

预期结果:

+-----+---------+-------+
| BRN |  Name   |  Amnt |
+-----+---------+-------+
| 1   | John    |  10   |
| 2   | Belly   |   4   |
| 3   | John    |  14   |
| 4   | John    |   5   |
+-----+---------+-------+

困难的部分是获得金额,因为它是任意的,更不用说Amnt中的值在这个结果中几乎毫无价值。

如果您需要不同的BRN,则查询将为SELECT DISTINCT BRN FROM invoice

您甚至可能会使用SELECT DISTINCT BRN, Name FROM invoice

中间步骤为SELECT BRN,Name FROM invoice GROUP BY BRN, Name

但是,如果您尝试在等式中包含Amnt,则查询将失败,因为数据库无法确定要显示的Amnt。

所以,你可以试试这个kludge:

SELECT a.BRN, a.Name, b.Amnt FROM invoice AS a LEFT JOIN invoice AS b ON a.BRN=b.BRN

但不保证Amnt会接受它。

希望有所帮助。

答案 2 :(得分:0)

enter image description here

enter image description here

请参阅此处在查询中使用GROUP BY与您的条件