How to concatenate strings without repeating?

Source: Internet
Author: User
For example, the following table structure is used: idleishuxingmiaoshu1 elephant eyes big 2 elephant eyes Round 3 elephant nose high 4 bear nose small 5 elephant legs long 6 bear legs thin 7 elephant mouth small second column and third column and fourth column there may be n types of columns... for example, the following table structure is available:
Id lei shuxing miaoshu
1. big elephant eyes
2. round elephants
3. tall elephant's nose
4. little bear's nose
5. long elephant legs
6. the legs are thin.
7. small elephant mouth

The second column, the third column, and the fourth column may have n cases. to find a php method that can meet the following requirements, each description must be a lei, covers three items in shuxing (less than three items have a few counts, such as bears) to generate different descriptions of an animal.

Example: the result of the above data is

Big Elephant eyes, high nose, long legs
The elephant's eyes are round, his nose is tall, his legs are long
Big Elephant eyes, high nose, small mouth
...
As many combinations can be made, they cannot be listed one by one. through the above examples, I hope my friends can understand it.

Reply content:

For example, the following table structure is available:
Id lei shuxing miaoshu
1. big elephant eyes
2. round elephants
3. tall elephant's nose
4. little bear's nose
5. long elephant legs
6. the legs are thin.
7. small elephant mouth

The second column, the third column, and the fourth column may have n cases. to find a php method that can meet the following requirements, each description must be a lei, covers three items in shuxing (less than three items have a few counts, such as bears) to generate different descriptions of an animal.

Example: the result of the above data is

Big Elephant eyes, high nose, long legs
The elephant's eyes are round, his nose is tall, his legs are long
Big Elephant eyes, high nose, small mouth
...
As many combinations can be made, they cannot be listed one by one. through the above examples, I hope my friends can understand it.

Builder mode http://www.runoob.com/design-pattern/builder-pattern.html

select lei,group_concat(concat(shuxing,miaoshu)) result from tb group by lei

In this way, you can return
Lei result
Big Elephant eyes, high nose, long legs, small mouth
The bear has a small nose and thin legs.
...
Returns results to the background separated by commas (,), and then three results are randomly spliced.
--- Comments to the above personal opinions ---

In fact, I still don't quite understand whether the subject is asking questions about php or SQL.
However, if I feel that SQL is not good at solving such problems, I suppose it is a programming problem.

Since php is not understood, we only provide solutions:
1. query all qualified data based on lei, for example:

SELECT lei, shuxing, miaoshu FROM animalWHERE lei IN ('elephant ', 'Bear ');

2. in the codeleiAttributes andshuxingAttribute creationgroup byThe operation is as follows:

{"Elephant": {"eyes": ["big", "round"], "nose": ["high"], "leg": ["long"], "mouth": ["small"]}, "Bear ":{...}}

3. perform Cartesian product operation on the data in step 2, done.

PS: Actually, the table structure is poorly designed and should be splitanimalTable (animal_id,animal_name) Andanimal_propTable (animal_prop_id,animal_id,prop_name,prop_desc).

You can use sub-tables to divide them into three tables. Table1: used to record lei information table2: used to record shuxing, table1 ID table3:

Used to record the IDs of miaoshu and table2, and used three tables for association query

Do you have any help?

Right. what I'm talking about when I go upstairs?
Table sharding
Then perform join query based on the table
Take the class out and create a table.
Create another table (one-to-multiple relationship)

Python version:

from collection import defaultdictanims = defaultdict(lambda : defaultdict(lambda : defaultdict(set)))for lei, shuxing, miaoshu 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(shuxings[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 jj in range(shuxings[j][1]):                            for kk in range(shuxings[k][1]):                                print shuxings[i][0], shuxings[i][1][ii], shuxings[j][0], shuxings[j][1][jj], shuxings[k][0], shuxings[k][1][kk]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.