For example, the following data table structure
Id Lei shuxing Miaoshu
1 Elephant eyes big
2 Elephant eyes round
3 Elephant Nose High
4 Bear's Nose small
5 Elephants with long legs
6 Legs of bear thin
7 Elephant Mouth Small
The second and third and fourth columns may have n multiple cases, and a PHP method that implements the following requirements is required for each description to be an lei, covering 3 items in shuxing (not enough three for several counts such as bears) to produce a different description of an animal
As an example, the result of the above data should be
The elephant has big eyes, high nose, long legs.
The elephant's eyes are round, the nose is tall, the legs are long
Elephants have big eyes, high noses, small mouths.
。。。
Due to the combination of a lot, it can not be listed, through the above examples hope that friends can understand
Reply content:
For example, the following data table structure
Id Lei shuxing Miaoshu
1 Elephant eyes big
2 Elephant eyes round
3 Elephant Nose High
4 Bear's Nose small
5 Elephants with long legs
6 Legs of bear thin
7 Elephant Mouth Small
The second and third and fourth columns may have n multiple cases, and a PHP method that implements the following requirements is required for each description to be an lei, covering 3 items in shuxing (not enough three for several counts such as bears) to produce a different description of an animal
As an example, the result of the above data should be
The elephant has big eyes, high nose, long legs.
The elephant's eyes are round, the nose is tall, the legs are long
Elephants have big eyes, high noses, small mouths.
。。。
Due to the combination of a lot, it can not be listed, through the above examples hope that friends can understand
Builder Mode http://www.runoob.com/design-pattern/builder-pattern.html
select lei,group_concat(concat(shuxing,miaoshu)) result from tb group by lei
This will return
Lei result
Elephants have big eyes, high noses, long legs, small mouths.
The bear has a small nose, thin legs.
...
Return to background by separating result with commas and then randomly stitching three results
---above personal insights, welcome to reviews---
In fact, I still do not understand the question of whether the Lord is asking the question of PHP or SQL.
But I don't think SQL is good at solving this kind of problem, so I'll assume it's a programming problem.
Because I do not understand PHP, so only to provide the idea of solving:
1. Query all eligible data according to the LEI, for example:
SELECT lei, shuxing, miaoshu FROM animalWHERE lei IN ('大象', '熊');
2, in the code of the lei
attributes and shuxing
attributes to do the group by
following:
{ "大象": { "眼睛": ["大大的", "圆圆的"], "鼻子": ["高高的"], "腿": ["长长的"], "嘴": ["小小的"] }, "熊": { ... }}
3, the data in 2 Cartesian product operation, done.
PS: In fact, the table structure is poorly designed and should be split into tables (, animal
animal_id
animal_name
) and animal_prop
tables (,,, animal_prop_id
animal_id
prop_name
prop_desc
).
The table can be divided into three tables. Table1: Information used to record an lei table2: The ID table3 used to record shuxing,table1:
Used to record the ID of the miaoshu,table2 and use three tables to correlate the query
Friends, can I help you to solve it?
Yes, it's a thought to go upstairs.
Sub-table
Then correlate the query against the table
Take the class out alone and build a table.
property of the class to build a table (1-to-many relationship)
Python version:
From collection Import defaultdictanims = Defaultdict (Lambda:defaultdict (Lambda:defaultdict (set))) for Lei, Shuxing, MI Aoshu in Db.select (' table ', [' Lei ', ' shuxing ', ' Miaoshu ']): Anims[lei][shuxing].add (Miaoshu) # enum all descsfor lei , v1 in Anims.items (): print ' desc-lei: ', lei shuxings = [(k, List (v)) for k,v in V1.items ()] shuxing_size = Len (shuxings) If shuxing_size = = 0:pass Elif shuxing_size = 1:for i in range (shuxing_size): For II in range (Shuxings[i][1]): Print shuxings[i][0], shuxings[i][1][ii] elif shuxing_size = = 2: For I in Range (Shuxing_size): for J in Range (I+1, shuxing_size): For II in range (s HUXINGS[I][1]): For JJ in Range (Shuxings[j][1]): Print shuxings[i][0], shuxings[ I][1][II], shuxings[j][0], SHUXINGS[J][1][JJ] else:for i in range (Shuxing_size): for J in range (I+1, Shuxing_size): for k in range (j+1, shuxing_size): For II in range (Shuxings[i][1]): for J J in range (Shuxings[j][1]): For KK in range (shuxings[k][1]): PRI NT Shuxings[i][0], Shuxings[i][1][ii], shuxings[j][0], SHUXINGS[J][1][JJ], shuxings[k][0], Shuxings[k][1][kk]