在MySQL中计算组合ID,然后将其存储为库存

时间:2018-12-19 11:09:04

标签: mysql sql join

好的,我正在用这个拔头发。

我有一个名为products_bind_variant的表

itemId int(10)
productId int(10)
variantId int(10)

variantId引用一种类型,例如大小,颜色等。

此表是变体

variantId int(10)
name varchar(150)
grouping varchar(150)

分组值应为“大小”,“颜色”,“范围”等

我的可变table数据在这里:

|1|Small|Size
|2|Medium|Size
|3|Large|Size
|4|Red|Colour
|5|White|Colour
|6|Blue|Colour
|7|Extra Large|Size
|8|1-3 years|Range
|9|3-10|Range

我的products_bind_variant数据在这里:

|1|69|5
|7|69|6
|8|69|8
|9|69|9
|10|69|4
|12|69|1
|18|69|3
|19|69|2
|20|69|7

我想查询自身的VariantId组合,只显示同一组组合的ID。

我尝试使用

SELECT a.productsid,
       a.variantid,
       b.variantid
FROM   products_bind_variant a
       INNER JOIN products_bind_variant b
               ON a.variantid < b.variantid

但是,由于它与自身匹配,因此返回36个结果。我需要考虑到仅在分组类型之间找到组合,即24种组合。

基本上,这类似于查询ID。

所以

69| 1,4,8(红色,1-3年小)

这里是我需要生成的组合,但没有名称,variantId

Blue, 1-3 Years, Extra Large
Blue, 3-10 Years, Extra Large
Blue, 1-3 Years, Large
Blue, 3-10 Years, Large
Blue, 1-3 Years, Medium
Blue, 3-10 Years, Medium
Blue, 1-3 Years, Small
Blue, 3-10 Years, Small
Red, 1-3 Years, Extra Large
Red, 3-10 Years, Extra Large
Red, 1-3 Years, Large
Red, 3-10 Years, Large
Red, 1-3 Years, Medium
Red, 3-10 Years, Medium
Red, 1-3 Years, Small
Red, 3-10 Years, Small
White, 1-3 Years, Extra Large
White, 3-10 Years, Extra Large
White, 1-3 Years, Large
White, 3-10 Years, Large
White, 1-3 Years, Medium
White, 3-10 Years, Medium
White, 1-3 Years, Small
White, 3-10 Years, Small

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

如果我的理解正确,那么您想要产品的每个分组的所有变体。如果是这样,请考虑聚合而不是JOIN

SELECT v.grouping,
       pbv.productsid,
       grouup_concat(pbv.variantid) as variants
FROM products_bind_variant pbv JOIN
     variant v
     ON pvb1.variantid = v1.variantid 
GROUP BY v.grouping;

答案 1 :(得分:0)

这将产生24行:

    `export class ApplicationComponent implements OnInit {

     applicationForm: FormGroup;
     constructor(private fb: FormBuilder, private route:Router, private activRoute:ActivatedRoute,
    private _ser: ApiServiceService) { 
    this.applicationForm = this.fb.group({
    //Application
      "IsNew": ['1'],
      "StudentID": ['', Validators.required,],
      "StudInfoID": [''],
      "UniversityID": [''],    // ?            
      "UniversityName": [''],  // ?      
      "CountryCode": [''],     // ?
      "DateOfApplication": ['', Validators.required,],
      "FirstName": [''],
      "MiddleName": [''],       // default null         
      "LastName": [''],
      "ApplicationStatusID": [''],    // ?           
      "Gender": [''],
      "DateOfBirth": [''],

    //personal Details                
      "PlaceOfBirth": [''],
      //Contact Details
      "EmailID": [''],
      "Handphone": [''],
      "Telephone": [''],
      "EmergencyContactName": [''],
      "EmergencyContactRelationShip": [''],
      "EmergencyContactTelephone": [''],
      //Address Details
      //Permanant Address
      "PermanentAddressCountryCode": [''],
      "permanentAddressState": [''],
      "PermanentAddressCity": [''],
      "PermanentAddress": [''],
      "PermanentAddressPostalCode": [''],
      //Mailing Address
      "MailingAddressCountryCode": [''],
      "MailingAddressState": [''],
      "MailingAddressCity": [''],
      "MailingAddress": [''],
      "MailingAddressPostalCode": [''],
      //Passport Details
      "PassportNumber": [''],
      "PassportIssueDate": [''],
      "PassportExpiryDate": [''],

    //Parent Details
      //Father Details
      "FatherFullName": [''],
      "FatherCitizenShip": [''],
      "FatherOccupation": [''],
      "FatherAddress": [''],
      "FatherPostalCode": [''],
      "FatherOfficeTelephone": [''],
      "FatherHomeTelephone": [''],
      "FatherHandPhone": [''],
      "FatherFaxNumber": [''],
      "FatherEmail": [''],
      //Mother Details             
      "MotherFullName": [''],
      "MotherCitizenShip": [''],
      "MotherOccupation": [''],
      "MotherAddress": [''],
      "MotherPostalCode": [''],
      "MotherOfficeTelephone": [''],
      "MotherHomeTelephone": [''],
      "MotherHandPhone": [''],
      "MotherFaxNumber": [''],
      "MotherEmail": [''],
      //Guardian Details
      "GuardianFullName": [''],
      "GuardianCitizenShip": [''],
      "GuardianOccupation": [''],
      "GuardianAddress": [''],
      "GuardianPostalCode": [''],
      "GuardianOfficeTelephone": [''],
      "GuardianHomeTelephone": [''],
      "GuardianHandPhone": [''],
      "GuardianFaxNumber": [''],
      "GuardianEmail": [''],

    //security questions
      "FirstSchoolName": [''],
      "MotherMedianName": [''],
      "FavoriteFood": [''],
      "FavoriteHoliday": [''],

    //medical disability needs
      "ChkmedicalconditionwhichmayaffectyourstudiesYes": [''],
      "ChkmedicalconditionwhichmayaffectyourstudiesNo": [''],
      "ChkHearing": [''],
      "ChkMedical": [''],
      "ChkVision": [''],
      "ChkMobility": [''],
      "ChkOther": [''],
      "Other": [''],
      "ChkWhatCICY": [''],
      "ChkWhatCICN": [''],

    //other Info
      "NationalServiceStatus": [''],
      "ORDDate": [''],
      "ExemptedReason": [''],
      "SpecialSkill": [''],
      "SpecialSkillExplain": [''],
      "ArrestedByPolice": [''],
      "ArrestedBypoliceExplain": [''],
      "DismissedFromEmployment": [''],
      "DismissedFromEmploymentExplain": [''],
      "MedicalCondition": [''],
      "MedicalConditionExplain": [''],
      "SubjectToSanction": [''],
      "SubjectToSanctionExplain": [''],
      "KnowAtSunrice": [''],
      "Source": [''],
      //co-curricular activities
      "DetailsOfECAMemberShip": [''],
      //English Language Proficiency
      "ChkEngLangFirstLanguageYes": [''],
      "ChkEngLangFirstLanguageNo": [''],
      "ChkIELTS": [''],
      "ChkTofel": [''],
      "ChkCute": [''],
      "ChkOtherEngLang": [''],
      "ChkGCEO": [''],
      "EngScoreGrade": [''],
      //sponsor details
      "ChktutionfeePaidByOrganisationY": [''],
      "ChktutionfeePaidByOrganisationN": [''],
      "OrganisationName": [''],
      "Status": [''],

    //Documents Required
      "ChkSPM": [''],
      "ChkALevelResult": [''],
      "ChkALevelCert": [''],
      "ChkDegreeResult": [''],
      "ChkDegreeCert": [''],
      "ChkPassportCopy": [''],
      "ChkReferenceLetter": [''],
      "ChkPersonalStatement": [''],
      "ChkIELTSResult": [''],
      "ChkCV": [''],
      "ChkPortfolio": [''],
      "ChkchkOthers": [''],

    // '?'               
      "DocumentsOthers": [''],
      "CreatedBy": [''],
      "UpdatedBy": [''],
      "CreatedDate": [''],
      "ModifiedDate": [''],
      "ApplicationNumber": [''],

    //new field 'parent-details'
      "fathersector": [''],
      "Mothersector": [''],
      "Gardiansector": [''],

    //for counselor '?'
      "EmpCode": [''],
      "Counsellor": ['', 
   [Validators.required,]],
      "CounsellorMobileNo": [''],

    //Children Details
      "FullName1": [''],
      "Ctizenship1": [''],
      "sex1": [''],
      "DateOfBirth1": [''],
      "countryofBirth1": [''],
      "placeofbirth1": [''],
      "FullName2": [''],
      "Ctizenship2": [''],
      "sex2": [''],
      "DateOfBirth2": [''],
      "countryofBirth2": [''],
      "placeofbirth2": [''],
      "FullName3": [''],
      "Ctizenship3": [''],
      "sex3": [''],
      "DateOfBirth3": [''],
      "countryofBirth3": [''],
      "placeofbirth3": [''],
      "FullName4": [''],
      "Ctizenship4": [''],
      "sex4": [''],
      "DateOfBirth4": [''],
      "countryofBirth4": [''],
      "placeofbirth4": [''],
      "FullName5": [''],
      "Ctizenship5": [''],
      "sex5": [''],
      "DateOfBirth5": [''],
      "countryofBirth5": [''],
      "placeofbirth5": [''],
      "FullName6": [''],
      "Ctizenship6": [''],
      "sex6": [''],
      "DateOfBirth6": [''],
      "countryofBirth6": [''],
      "placeofbirth6": [''],
      "Identity": [''],

    //Documents for under 18
      "chkguardianslip": [''],
      "chkhomestay": [''],
      "chkapprovedhostelaccomodation": [''],
      "chkwelfareform": [''],
      "chkCAAW": [''],
      "chkform1223": [''],
      "chkstudent": [''],
      "chkfather": [''],
      "chkmother": [''],
      "chkcourtletter": [''],
      "chkdeceasedletter": [''],
      "chkstudbirthcertificate": [''],
      "chkoyher1": [''],
      "txtother1": [''],

    // personal details
      "MaillingAddSameAsPermanat": [''], 
    //Address details

    //Application 
      "IC_No": ['', [Validators.required,]],
      "Counsellor2": [''],
      "EmpCode2": [''],
      "Counsellor3": [''],
      "EmpCode3": [''],

    // '?'
      "RejectedStatusRemarks": [''] 
    });
  }`

变量表中只有9个行是唯一的productid:3种颜色,2种范围和4种尺寸

select
      `colour`, `range`, `size`, cid, sid, rid
from (select variantId cid, name `colour` from  variant where grouping = 'Colour') c
cross join (select variantId sid, name `size` from  variant where grouping = 'Size') s
cross join (select variantId rid, name `range` from  variant where grouping = 'Range') r

+----+--------+-----------+-------------+-----+-----+-----+
|    | colour |   range   |    size     | cid | sid | rid |
+----+--------+-----------+-------------+-----+-----+-----+
|  1 | Red    | 1-3 years | Small       |   4 |   1 |   8 |
|  2 | White  | 1-3 years | Small       |   5 |   1 |   8 |
|  3 | Blue   | 1-3 years | Small       |   6 |   1 |   8 |
|  4 | Red    | 1-3 years | Medium      |   4 |   2 |   8 |
|  5 | White  | 1-3 years | Medium      |   5 |   2 |   8 |
|  6 | Blue   | 1-3 years | Medium      |   6 |   2 |   8 |
|  7 | Red    | 1-3 years | Large       |   4 |   3 |   8 |
|  8 | White  | 1-3 years | Large       |   5 |   3 |   8 |
|  9 | Blue   | 1-3 years | Large       |   6 |   3 |   8 |
| 10 | Red    | 1-3 years | Extra Large |   4 |   7 |   8 |
| 11 | White  | 1-3 years | Extra Large |   5 |   7 |   8 |
| 12 | Blue   | 1-3 years | Extra Large |   6 |   7 |   8 |
| 13 | Red    | 3-10      | Small       |   4 |   1 |   9 |
| 14 | White  | 3-10      | Small       |   5 |   1 |   9 |
| 15 | Blue   | 3-10      | Small       |   6 |   1 |   9 |
| 16 | Red    | 3-10      | Medium      |   4 |   2 |   9 |
| 17 | White  | 3-10      | Medium      |   5 |   2 |   9 |
| 18 | Blue   | 3-10      | Medium      |   6 |   2 |   9 |
| 19 | Red    | 3-10      | Large       |   4 |   3 |   9 |
| 20 | White  | 3-10      | Large       |   5 |   3 |   9 |
| 21 | Blue   | 3-10      | Large       |   6 |   3 |   9 |
| 22 | Red    | 3-10      | Extra Large |   4 |   7 |   9 |
| 23 | White  | 3-10      | Extra Large |   5 |   7 |   9 |
| 24 | Blue   | 3-10      | Extra Large |   6 |   7 |   9 |
+----+--------+-----------+-------------+-----+-----+-----+

使用的数据:

SELECT
    b.productid, v.grouping, group_concat(v.name order by v.name separator ',') as variants
FROM products_bind_variant b
inner join variant v on b.variantid = v.variantid
group  by b.productid, v.grouping

+----+-----------+----------+--------------------------------+
|    | productid | grouping |            variants            |
+----+-----------+----------+--------------------------------+
|  1 |        69 | Colour   | Blue,Red,White                 |
|  2 |        69 | Range    | 1-3 years,3-10                 |
|  3 |        69 | Size     | Extra Large,Large,Medium,Small |
+----+-----------+----------+--------------------------------+