I've currently got three tables to track changes on specific entries but it seems like I am ending up with a ton of entries and I am not sure if that is the best possible way.
My first table holds the basic information and the second and third one the extra entries I grab every 8 hours.
ID | creation_date | removal_date | article_url | status which are basically the most stable entries. Status and removal_date are the only ones that will change in case we disable/remove an entry.
Example:
ID | creation_date | removal_date | article_url | status
---|------------------|------------------|-------------|-------
1 | 10/01/2020 20:00 | NULL | http://xxx | 1
2 | 23/01/2020 10:00 | 27/01/2020 13:00 | http://xxx2 | 2
3 | 10/02/2020 15:00 | NULL | http://xxx3 | 1
Status 1 = Active
Status 2 = Inactive
The second table holds everything else:
ID | main_id | last_update | title | description | views | rating | comments
The second table creates a new entry every 8 hours as long as something changes. Then based on the entries added here, I show average views/rating/comments changes on a daily/weekly/monthly basis.
Example:
ID | main_id | last_update | title | description | views | rating | comments
---|---------|------------------|----------------|--------------------|-------|--------|---------
1 | 1 | 10/01/2020 20:00 | First Article | Description.. | 1 | 1 | 0
2 | 2 | 23/01/2020 10:00 | Second Article | Desc.. | 1 | 1 | 0
3 | 1 | 11/01/2020 20:00 | First Article | Description update | 15 | 3 | 2
4 | 1 | 12/01/2020 20:00 | 1st Article | Description update | 30 | 5 | 4
5 | 3 | 10/02/2020 15:00 | 3rd Article | Descript! | 3 | 1 | 1
The third table holds the tags:
ID | main_id | tag_id | date_added | date_removed
I thought instead of having a status to add an empty date_removed so in case the tags get updated/removed/etc update that part. The tags are saved in a separate table and just grab the id and store the connection between the two here.
Example:
ID | main_id | tag_id | date_added | date_removed
---|---------|--------|------------------|------------------
1 | 1 | 2 | 10/01/2020 20:00 | NULL
2 | 1 | 3 | 15/01/2020 16:30 | 17/01/2020 13:00
3 | 2 | 3 | 23/01/2020 10:00 | NULL
4 | 3 | 5 | 10/02/2020 15:00 | NULL
5 | 1 | 5 | 11/02/2020 17:00 | NULL
I'd just like to know if there is a better / more proper way to store the above data.
Yepp, #Maria, is clearer.
Assuming that you are dealing with blog entries, you may have a data model like this.
Table 1. articles. // where every article is created.
article_id | article_creation_date | article_title | article_url | article_creator_id | article_description |
-----------|-----------------------|---------------|-------------|--------------------|------------------------------|
1 | 2020/03-31 10:36:05 | "The Dilemma" | /articles/1 | 23 | Explains the relations....|
Table 2. article_status // stores changes of state to each article.
article_status | article_id | status | date_of_change |
---------------|------------|--------|--------------------|
1 | 1 | 7 | 15/04/2020 09:30:00|
Table 3. article_tags. // every article and it's tags
article_tag | article_id | tag_id | date_added |
------------|------------|--------|--------------------|
1 | 1 | 24 | 15/04/2020 09:30:00|
Table 4. article views // stores the summarized amount of views to each article, for a period, say day, week, 8 hours,...
article_v_id | article_id | views_summarized | time_lapse | time_lapse_value | date_summarization |
-------------|------------|------------------|----------------|-------------------|-------------------|
1 | 1 | 1578 | Day | 10/04/2020 | 12/04/2020 13:27:04 |
Table 5. article_updates // stores changes/updates made to each article.
article_update_id | article_id | type_of_update | update_detail | update_author | date_of_update |
------------------|------------|--------------------------------|---------------|------------------------|
1 | 1 | Title | | John Doe | 19/04/2020 15:27:24 |
And the contentn of the update is stored directly on articles table, say change of title. NO need to store all modified titles, content. Just the event and who made the change.
Related
I have a tracking table, the content of the tracking table contains products. I want to list the most followed products with procedure How can I do this?
Follow-up chart
| ID |KullaniciID| StokKodu | Tarih |
| 1 | 1 | AXERA | 2023-02-01 15:10:53.193 |
| 2 | 1 | ERKMF | 2023-02-02 10:27:17.373 |
| 3 | 2 | AXERA | 2023-02-02 10:27:16.643 |
| 4 | 3 | AXERA | 2023-02-06 16:27:28.873 |
In this table given as an example, I want to list 5 of the most followed products according to the stock code, how do I do this?
For some reason I am having difficulty wording this question but I will try my best. I've been searching for 2 days on and off now and haven't found a good solution to the issue.
I have a Table called InventoryNode;
_________________________________________________
| InvID | ID | Slot | ItemID1 | ItemID2 | ItemID3 |
|-------|----|------|---------|---------|---------|
| 1 | 1 | Neck | 10 | 22 | 66 |
| 1 | 2 | Head | 26 | 23 | 56 |
| 1 | 3 | Leg | 19 | 21 | 76 |
And another table called Inventory which stores the Node ID in each column
_____________________________
| ID| Neck | Head | Leg | ... |
|---|------|------|-----|-----|
| 1 | 1 | 2 | 3 | 66 |
If there a way I can insert the node ID's into the Inventory table based off the InvID and populate all the columns with the correct name with the Node's ID?
Something like this?
INSERT INTO Inventory INNER JOIN InventoryNode ON
(Inventory.ID = InventoryNode.InvID) WHERE Inventory.column_name =
InventoryNode.Slot SET InventoryNode.InvID
Working in Redmine, I need to copy(not move) data from certain rows to other rows based on matching project id numbers with time entries.
I have included a diagram of the table "custom_values" and my understanding of the design below(CURRENT DATA):
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | |
+----+-----------------+---------------+-----------------+-------+
At the risk of oversimplifying,
"id" = The primary key for each entry in custom_values
"customized_type" = Specifies which db table the row is referring to.
"customized_id" = Specifies the primary key for the db table entry previously specified in "customized_type".
"custom_field_id" = Specifies which custom field the row is referring to. Redmine admins can arbitrarily add and remove custom fields.
"value" = The data contained within the custom field specified by
"custom_field_id"
In my situation, the values listed in "value" are representing unique customer id numbers. The customer id numbers did not always get entered with each time entry. I need to copy the customer numbers from the project rows to the matching time entry rows. Each time entry has a project_id field.
So far, here is my mangled SQL query:
SELECT
custom_field_id,
custom_values.value AS 'CUSTOMER_NUMBER',
custom_values.customized_id AS 'PROJECT_ID_NUMBER',
custom_values.customized_type,
time_entries.comments AS 'TIME_ENTRY_COMMENTS'
FROM
redmine_tweaking.custom_values
LEFT JOIN
redmine_tweaking.time_entries ON custom_values.customized_id = time_entries.project_id
WHERE
custom_values.customized_type='Project' AND custom_values.custom_field_id=1;
The query I have so far allows me to see that I have the time entries connected properly to their matching projects, but that is all I have been able to figure out. So in other words, this SQL statement does not exactly solve my problem.
Plus, even if it did work, I think the way I laid it out looks like 200 lbs of bird poop. There must be a better/more optimized way to do this.
Any help would be greatly appreciated. I am relatively new and I have been pouring hours into solving this problem.
UPDATE:
Ok, here is the time_entries table:
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| id | project_id | user_id | issue_id | hours | comments | activity_id | spent_on | tyear | tmonth | tweek | created_on | updated_on |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | .25 | test | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 08:18:12 | 2015-11-04 10:18:12 |
| 2 | 2 | 1 | 1 | .25 | test2 | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 09:18:12 | 2015-11-04 12:18:12 |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
As opposed to the original table that I first posted, the expected output would show this:
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | 03 |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | 04 |
+----+-----------------+---------------+-----------------+-------+
In a simple sales sample I have 3 primary tables: Order, Product, Orders_Products:
Product table
+----+-----------+
| id | name |
+----+-----------+
| 1 | A |
| 2 | B |
+----+-----------+
order table
+----+---------+------------+------------+---------------------+
| id | user_id | total_cost | order_date | status_id |
+----+---------+------------+------------+---------------------+
| 1 | 5 | 25.00 | 2012-02-03 23:30:24 | 1 |
| 2 | 7 | 30.00 | 2012-02-13 18:06:12 | 1 |
+----+---------+------------+------------+---------------------+
orders_products table
+----+----------+------------+--------+
| id | order_id | product_id | cost |
+----+----------+------------+--------+
| 1 | 1 | 34 | 10.00 |
| 2 | 1 | 25 | 10.00 |
| 3 | 1 | 27 | 2.50 |
| 4 | 1 | 36 | 2.50 |
| 5 | 2 | 75 | 25.00 |
| 6 | 2 | 74 | 5.00 |
+----+----------+------------+--------+
But in my system:
A user adds money into his account then he can spend it
Products are services like Product A: user can post 5 Ads so when user post a Ads that 5 become 4 and so on.
How should I design DB for it??
You currently have no table to store customer data. I would strongly advise creating one using an Auto-Increment ID field, a name field and a field to store the money on their account which can be modified when the add money to it or purchase something.
In the Order table, the user_id field should be a foreign key from the Customer table I just told you how to create.
If you want to better understand what you should be doing, I would recommend looking at the Entity-Relationship Model
I don't entirely understand what your second question is, can you explain it in more detail? Are all the products just a set number of ads that the customer can post?
I have three tables:
users:
+----------+-----------------------------+-----------+
| users_id | user detail | otherID |
+----------+-----------------------------+-----------+
| 1 | user name or details | 1 |
| 2 | user name or details | 1 |
| 3 | user name or details | 4 |
| 4 | user name or details | 1 |
| 5 | user name or details | 21 |
| 6 | user name or details | 2 |
+----------+-----------------------------+-----------+
photos:
+----------+----------------+-----------+--------+---------+--------------+
| photosID | url | title | userID | likes | remarksID |
+----------+----------------+-----------+--------+---------+--------------+
| 1 | 7459.JPG | TITLE | 1 | 150 | 255 |
| 2 | 7510.JPG | TITLE | 1 | 146 | 247 |
| 3 | 7460.JPG | TITLE | 2 | 2 | 56 |
+----------+----------------+-----------+--------+---------+--------------+
remarks:
+-----------+---------------------------------------+---------+------------+
| remarksID | remark | userID | photoID |
+-----------+---------------------------------------+---------+------------+
| 1 | REMARKS for PhotoID 1 | 1 | 1 |
| 2 | REMARKS for PhotoID 1 | 1 | 1 |
| 3 | REMARKS for PhotoID 1 | 1 | 1 |
| 4 | REMARKS for PhotoID 2 | 1 | 2 |
| 5 | REMARKS for PhotoID 2 | 1 | 2 |
| 6 | REMARKS for PhotoID 3 | 2 | 3 |
+-----------+---------------------------------------+---------+------------+
I am trying to extract the remarks for a given users photo, but only want the photo to appear once but all the relevant remarks for the photo to be displayed for each users photo.
If there are no remarks then just display the photo without remarks.
The layout would look like this on a web page-
------------------------------------
Photograph 1
Remarks 1
Remarks 2
Remarks 1
------------------------------------
Photograph 2
NO remarks for photo
------------------------------------
Photograph 3
Remarks 1
Remarks 2
Remarks 3
Remarks 4
------------------------------------
Photograph 4
Remarks 1
Remarks 2
------------------------------------
Is my assumption correct that your difficulty is, how to get the database to do this? If so, then the answer is that this function (formatting the data), is not (or should not) generally be the responsibility of the database. Fetch the photos from the database as one result set, then fetch the matching comments in another, and let your application or reporting tool format the data the way you want it.