当状态更改为与上一个相反的值时,获取按行分组的行

时间:2018-07-22 09:14:44

标签: mysql

我有桌子:

<script type="text/babel">
      class List extends React.Component {
          constructor(props) {
              super(props);
              this.state = {
                  items: this.props.items,
              }
              this.handleClick = this.handleClick.bind(this);
          }

      handleClick(itemName) {
          const givenItemSet = this.state.items.slice();
          var newItemList = givenItemSet.map(el => {
                      if(el.name === itemName)
                         return Object.assign({}, el, {votes: el.votes + 1})
                      return el
                  });

          this.setState({
            items: newItemList
          })
      }

      render() {
          return (
              <div>
                  <h1>Vote Your JS Library!</h1>
                  <ul>
                      { this.state.items.map((item) => 
                          <li key = {item.name}>
                              {item.votes}
                              {item.name}
                              <button onClick = {this.handleClick.bind(this, item.name)}> + </button>
                          </li>
                      ) }
                  </ul>
              </div>
          )
      }
    }

    const listItems = [{
      name: 'React',
      votes: 0
    },
    {
      name: 'Vue',
      votes: 0
    },
    {
      name: 'Angular',
      votes: 0
    },
    {
      name: 'Ember',
      votes: 0
    }]

    ReactDOM.render(<List items= { listItems } />, document.getElementById('root'));
    </script>

当状态更改时,我只希望第一次出现1和2(我的意思是0和3对我并不重要)-因此,在这种情况下,我应该获得ID:1、4、10。但只会将所有值按1或2进行分组,而不仅仅是状态更改的情况。 任何想法请如何指定mysql查询?

2 个答案:

答案 0 :(得分:0)

所以,今天很慢...

SELECT MIN(id)
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev=status THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=status prev 
         FROM 
            ( SELECT * 
                FROM my_table 
               WHERE status IN (1,2)
            ) x 
         JOIN 
            ( SELECT @prev:=1,@i:=1 ) vars 
        ORDER 
           BY id
     ) a
 GROUP
   BY i;

答案 1 :(得分:0)

SELECT id
     , status
     , if( status = @switch
          and if( @switch = 1, @switch:=2, @switch:=1 ) in(1,2)
         , 1
         , 0
         ) sw 
  FROM status
    JOIN( SELECT @switch :=1 ) t
  WHERE status in (1,2)
  HAVING sw=1
  ORDER BY id