SQL不同的分组查询

时间:2018-05-31 19:33:50

标签: mysql sql

样本表格数据

id    id_order    name    phone    price
1       4E0        A       789      $100
2       4E0        A       789      $100
3       4LK        A       789      $200
4       2LP        B       420      $50
5       DK2        B       420      $80

我希望结果类似于不同的行(id_order),其中phone =" 789"

    id    id_order    name    phone    price
    1       4E0        A       789      $100
    3       4LK        A       789      $200

   id    id_order    name    phone    price
   2       4E0        A       789      $100
   3       4LK        A       789      $200

我尝试了这个,但没有获得所需的输出:

SELECT DISTINCT (id_order), * from table_name WHERE phone= "789";

2 个答案:

答案 0 :(得分:3)

select 
    min(id) id,    
    id_order,    
    name,    
    phone,    
    price 
  from yourtable 
  group by id_order, name, phone, price

答案 1 :(得分:1)

要始终为每个id_order返回一行 - 即使名称,电话或价格不同 - 也有一个返回每个id_order的最低ID的子查询。结果为const initialState = { isLoading: false, // loadingStatus: 'READY', //是否下载成功 error success detailData: {}, headerData: { mainImage:"", displayName:"", signature: "Hello, I am Olivia! Nice to meet you:)", gender: 0, age: 0, height: 161, }, basicInfoData:[], imageData:[ ], descriptionData:[], tagData:[], }; // reducer case DT_UPLOAD_IMAGE_LIST_SUCCESS: console.log('tyu',action.data); return Object.assign({}, state, { imageData:action.data.data, }); default: return state; //then MapStateToProps function mapStateToProps(state) { console.log(state.dtUserInfoEditReducer.imageData); return { isLoading: state.dtUserInfoEditReducer.isLoading, loadingStatus: state.dtUserInfoEditReducer.loadingStatus, detailData: state.dtUserInfoEditReducer.detailData, headerData: state.dtUserInfoEditReducer.headerData, basicInfoData: state.dtUserInfoEditReducer.basicInfoData, imageData: state.dtUserInfoEditReducer.imageData, descriptionData: state.dtUserInfoEditReducer.descriptionData, tagData: state.dtUserInfoEditReducer.tagData, }; } // image show component const SPImageGallery = ({imageData, title}) => { console.log(imageData); return( <DetailDiv> <TitleDiv>{title}</TitleDiv> <Gallery images={imageData.map(({ id, imageUrl, index }) => ({ src: imageUrl, orientation:'landscape', useForDemo: true }))} showThumbnails /> </DetailDiv> ) } // gallery const gallery = images.slice(0,14).map((obj, i) => { //console.log('71',obj); return ( <a href={obj.src} className={css(classes.thumbnail, classes[obj.orientation])} key={i} onClick={(e) => this.openLightbox(i, e)} > <img src={obj.src} alt=""/> </a> ); });

JOIN

执行为:

select t1.*
from table_name t1
join (select min(id) minid, id_order
      from table_name
      group by id_order) t2
    on t1.id = t2.minid and t1.id_order = t2.id_order
where t1.phone = 789