Database structure for multiple values [duplicate] - mysql

This question already has answers here:
have address columns in each table or an address table that is referenced by the other tables?
(13 answers)
Closed 6 years ago.
I have the following database structure:
categories:
cat_id | name
-------+--------
1 | test 1
2 | test 2
date:
id | text | cat_id
---+--------+--------
1 | google | 1
2 | fb | 2
3 | yahoo | 1,2
I want to display "yahoo" in both categories. I know it's not good to store two values in the same cell. My last idea is to insert two rows with same text but different cat_id.
Is there any other way to do this?

You can have it like this:
categories:
cat_id | name
-------+--------
1 | test 1
2 | test 2
date:
id | text | cat_id
---+--------+--------
1 | google | 1
2 | fb | 2
3 | yahoo | 1
4 | yahoo | 2

Related

Mysql: Conditions on a unique primary key tuple

Short
Is it possible, that if I have a unique primary key tuple (idUser,idRep) that for each idUser n only certain combinations with idRep are possible?
For example that the following groups of keys are allowed:
(n,1);(n,2);(n,3) OR (n,1);(n,6) OR (n,2);(n,5) OR (n,4);(n,3) OR (n,7)
but not the keys (n,1) and (n,7) together?
Long question
I have the following table
idUser | idGroup | idType |
where the primary key is (idUser,idGroup). There are three different values for idGroup: 1,2,3. For almost all users the idType is always the same independent of the group, but there are a few exceptions. Thus the table looks like that
idUser | idGroup | idType
--------------------------
1 | 1 | 43
1 | 2 | 43
1 | 3 | 43
2 | 1 | 22
2 | 2 | 22
3 | 1 | 12
3 | 3 | 12
4 | 2 | 5
4 | 3 | 6
I thought I could avoid the redundancy with a column idRep which contains the information in which groups one user is as follows
1 -> user is in group 1
2 -> user is in group 2
3 -> user is in group 1 and 2 (1+2=3)
4 -> user is in group 3
5 -> user is in group 1 and 3 (4+1=5)
6 -> user is in group 2 and 3 (4+2=6)
7 -> user is in group 1 and 2 and 3 (4+2+1=7)
this would reduce redundancy and the above table would shrink to
idUser | idRep | idType
-------------------------
1 | 7 | 43
2 | 3 | 22
3 | 5 | 12
4 | 2 | 5
4 | 4 | 6
However the disadvantage is that its possible that even if (idUser,idRep) is a primary key there is the possibility of a wrong entry like
idUser | idRep | idType
-------------------------
1 | 7 | 43
1 | 1 | 42
In this case, there is a contradiction about idType for idUser 1 in group 1.
So my question is: Is it possible to say that for a specific idUser n only the following groups of keys are allowed:
(n,1);(n,2);(n,3) OR (n,1);(n,6) OR (n,2);(n,5) OR (n,4);(n,3) OR (n,7)
but not for (n,1) and (n,7)?

Compare field values in same row SQL [duplicate]

This question already has answers here:
mysql select lowest price from multi select
(4 answers)
Closed 7 years ago.
table structure
id | name | date_usa | date_jpn | date_uk
---+------+----------+----------+--------
1 | a | 01/2015 | 12/2014 | 04/2015
2 | b | 05/2015 | 05/2015 | 05/2015
3 | c | 08/2016 | 09/2017 | 09/2017
I want it to list out:
earliest release dates:
a: 12/2014
b: 05/2015
c: 08/2016
How can I achieve this?
select name, least(date_usa, date_jpn, date_uk)
from your_table

Get related posts by tags

Hi I'm very bad at writing mysql queries (I'm working on it).
Could you guys help me with this one?
I have a table of tags:
id | tag
--------------------------------
1 | css
2 | c++
3 | perl
4 | sql
5 | pyhton
Another table of PostsA_tags:
id | postID | tag
--------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 3
5 | 3 | 3
Another table of PostsB_tags:
id | postID | tag
--------------------------------
1 | 1 | 2
2 | 2 | 3
3 | 2 | 1
4 | 3 | 4
5 | 3 | 5
Another table of PostA:
postID | info
--------------------------------
1 | this
2 | is
2 | infor
3 | mation
4 | lol
Another table of PostB:
postID | info
--------------------------------
1 | more
2 | infor
3 | mation
4 | please
5 | hahaha
So now, the challenge is to order B posts from the A posts.
This means that if Peter is the owner of A posts, we need to get all his tags from all his posts. In this case it would be:
css, c++, perl
While, Sam is the owner of B posts. Now we need to order Sam's posts (B posts), by the amount of coincidences between Peter's tags (A tags) and the tags from each Sam's post.
In this case it would be:
B Posts ordered by coincided factor DESC
postID | info
--------------------------------
2 | infor
1 | more
3 | mation
4 | please
5 | hahaha
I'm really stuck. I know how to get Sam's tags. But not how to measure the coincidence factor between Sam's tags and the tags from each Peter's post.
Sorry for my english :S
Thanks in advance
Here's a fiddle... sqlfiddle.com/#!2/8450c/3
I think I have resolved the problem.
Here is the query
SELECT *, SUM(`PostsA_tags`.`tag` = `PostsB_tags`.`tag`) as rel FROM `PostsB_tags`
LEFT OUTER JOIN `PostsA_tags` `PostsA_tags` ON(`PostsA_tags`.`tag` = `PostsB_tags`.`tag`)
GROUP BY `PostsB_tags`.`postID`
ORDER BY rel DESC

Data Entry Tracking (Database Design)

I have developed a website (PHP) that allow staffs to add records on to our system.
Staffs will be adding thousands of records into our database.
I need a way to keep track of what record have been done and the process/status of record.
Here a number of Teams I could think of:
Data Entry Team
Proof Reading Team
Admin Team
When staff (Data Entry Team) completed a record - he/she will then click on the Complete button. Then somehow it should asssign to 'Proof Reading Team' automatically.
A record need to be checked twice from a Proof Reading Team. If StaffB finish proof reading then another member from Proof Reading Team need to check it again.
When Proof reading is done, Admin Team will then assign "Record Completed"
In a few months later record might need to be updated (spelling mistake, price change, etc) - Admin might to assign record to Data entry team.
Is this good data entry management solution? How do I put this into Database Design perspective?
Here what I tried:
mysql> select * from records;
+----+------------+----------------------+
| id | name | address |
+----+------------+----------------------+
| 1 | Bill Gates | Text 1 Text Text 1 |
| 2 | Jobs Steve | Text 2 Text 2 Text 2 |
+----+------------+----------------------+
mysql> select * from staffs;
+----+-----------+-----------+---------------+
| id | username | password | group |
+----+-----------+-----------+---------------+
| 1 | admin1 | admin1 | admin |
| 2 | DEntryA | DEntryA | data_entry |
| 3 | DEntryB | DEntryB | data_entry |
| 4 | PReadingA | PReadingA | proof_reading |
| 5 | PReadingB | PReadingB | proof_reading |
+----+-----------+-----------+---------------+
mysql> select * from data_entry;
+----+------------+-----------+------------------------+
| id | records_id | staffs_id | record_status |
+----+------------+-----------+------------------------+
| 1 | 2 | 3 | data_entry_processiing |
| 2 | 2 | 3 | data_entry_completed |
| 3 | 2 | 4 | proof_read_processing |
| 4 | 2 | 4 | proof_read_completed |
| 5 | 2 | 5 | proof_read_processing |
| 6 | 2 | 5 | proof_read_completed |
+----+------------+-----------+------------------------+
Is there alternative better solution of database design?
i think design it's well done. but may be you want to separate group into groups table, and record_status into status table. If you're storing a lot of records you would store a lot of useless information, at least create an enum type for record_status field and group field
table: groups
id - name 1 - admin 2 - data_entry 3 - proof_reading
...
table: status
id - name 1 - data_entry_processing ...
and if you want the users to be in different groups at a time, you could create users_group table
table: user_groups
group_id - user_id 1 - 1 2 - 1 1 - 4 3 -
4 4 - 4 ....
Hope this helps

Special unique columns

I have a situation where a website (a source) has multiple feeds (category separated)
Feed Table
feed_id | source_id | feed_url
1 | 1 | http://example.com/rss?category=1
2 | 1 | http://example.com/rss?category=5
3 | 2 | http://textample.com/rss
Item Table
item_id | true_id | feed_id
1 | 1332 | 1
2 | 76549 | 1
3 | 76549 | 2
4 | 76549 | 3
the true id is the id I try to get from the source site.
I want the item id 2 & 3 are the same, because they share the same source (example.com), item 4 is not the same because it has a different source (textample.com)
Is there a way I can enforce that consistency, without adding the source id to the Item Table?
RUN
ALTER TABLE `itemTable` ADD UNIQUE (`feed_id`);