This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to use comma-separated values in a DB Column?
I want to create similar articles script. My idea is that articles would be similar by ID. For example if article have ID = 1 for this article, similar article will be with similar_id = 1
Table example:
ID Similar_ID
1 0
2 0
3 1
4 1
Article with ID 1, will have 2 extra articles with ID 3 and 4.
How to model table if there are more than similar ID ?
Example:
ID Similar_ID
1 0
2 0
3 1,2
4 1,2,3
You should use two tables, the original one you have above, and a separate one that has one row per Article-Similar Article.
So your original table would be: Articles
ID Content
1 bla
2 blah
3 etc.
4 whatever
And your other table would be as you have above: ArticlesSimilar
ID Similar_ID
3 1
3 2
4 1
4 2
4 3
You would ensure that the combination ID-Similar_ID were UNIQUE.
Related
This question already has answers here:
What are the options for storing hierarchical data in a relational database?
(7 answers)
Closed 8 months ago.
I am saving data with unknown number of branches in JSON form for now and children can have unknown number of children.
example1 - {employee_1:{employee_2:{employee_3:{}}}}
example2 - {employee_3:{employee_2:employee_1:{}}, employee_4:{employee_3:{}}}
example hierarchical structure
Now to fetch this data, i have to first fetch this hierarchy saved as json and then fetch the id's stored in this json
is there any way to fetch this in one query?
or is there better approach to store and retrieve this?
i am using MySQL
Employee Table
id
name
1
Alice
2
Bob
3
Emily
4
Brian
Example 1 Employee Relation Table
id
parent_id
child_id
1
2
3
2
1
2
Example 2 Employee Relation Table
id
parent_id
child_id
1
4
3
2
2
1
3
3
4
4
3
2
What I Have:
I have a table where I have two basic columns on which the query is supposed to work
c1|c2
1 2
2 1
1 3
3 1
2 4
Now I want the result after the execution of the query is this
c1|c2
2 1
3 1
Notice
(3,1) was the last occurence of 1 and 3 no matter in which order they are as along as it is 1 and 3 and same is (2,1)
How can I achieve this where one value of the two columns is static for example in this case (1) is static and I want only those rows which has (1) in any of the two columns in their last occurrence.. Can any one help ?
This question already has answers here:
Possible to do a MySQL foreign key to one of two possible tables?
(6 answers)
Closed 8 years ago.
I am fumbling my way through phpMyAdmin and mySQL. I’m creating something where a customer can register multiple products. The data coming in is:
Category > Type > Size > Color
So for example:
Cookware > Oven > 5 qt > Blue
Bakeware > Casserole > 3qt > Blue
Accessories > Textiles > N/A > Blue
Etc.
I have set up one table with categories, and 4 tables to cover each product type.
Categories
ID Category
1 Cookware
2 Bakeware
3 Accessories
4 Serveware
Cookware Table
ID Type
1 Oven
2 Skillet
3 Roaster
Bakeware Table
ID Type
1 Casserole
2 Pie Dish
3 Baker
Etc.
Then, in the registration table, I set up a foreign key to link the category to the category table. So it would look something like this:
ID CustID Category Type Size Color
1 20 2 1 11 34
1 20 1 1 9 34
(sorry the formatting is so terrible! not sure how to fix)
But, I’m stuck on how to link the product type to the correct product type table since it is dependent on which category they picked. Hopefully this makes sense. Maybe I don't even need to link them and can still request the data somehow through a query?
Merge the last 2 tables ?
ID Type Category
1 Oven 1
2 Skillet 1
3 Roaster 1
4 Casserole 2
5 Pie Dish 2
6 Baker 2
How can we update table 1 such that it will replace b field value of table 1 by that of table 2 where a field value are found same?
Suppose I have two tables
table 1
fields a b c
1 5 10
1 5 8
2 5 0
1 4 11
and
table 2
fields a b
1 6
1 7
2 5
1 4
I'm going on 6th form knowledge so I'll leave the code for you to do, but here's basically how I'd do it:Select all values from table 2For each value, select the rows from table 1 with the matching 'a' valueCount number of matching valuesIf it's over one, update table 1 set 'b' as the new value where 'a' matches
Edit: Oh, just realised, the 'a' values aren't unique, unless both tables have matching ID for each row, I'm not sure you can do it.
This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 9 years ago.
I am trying to use the stuff function in MS SQL to stuff certain info. Here is the example:
Number Value
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
I would like to stuff the column so that only one record will display as following:
Value Number
1 1,2,3
2 1,2,3
3 1,2
Please note that there are a like n-Numbers and n-Values.
You can use GROUP_CONCAT for this. For example:
SELECT `Value`, GROUP_CONCAT(DISTINCT `Number` ORDER BY `Number`)
FROM `yourTable`
GROUP BY `Value`