update mysql records with mapping tables in multiple rows - php

I have create a simple blog system, the following three tables in my mysql database.
when the author update the article the new tag names are passed dynamically. for example the tags is not predefined. author create new tag when the article written i have completed successfully, but update the article the new tags and existing tags i want update appropriate articles in article table, tag is new then create new tag in tag table and mapping update with tag_hook table
Table 1: article
-----------------------------------
| article_id | title | desc |
-----------------------------------
| 1 | php mysql | lorem |
-----------------------------------
| 2 | java oops | lorem |
-----------------------------------
Table 2: tag
--------------------------
| tag_id | tag_name |
--------------------------
| 1 | sql |
--------------------------
| 2 | java |
--------------------------
| 3 | python |
--------------------------
| 4 | ruby |
--------------------------
Tbale 3: tag_hook
-----------------------------------------------
| hook_id | tag_id | article_id | type |
-----------------------------------------------
| 1 | 1 | 1 | article |
-----------------------------------------------
| 2 | 1 | 2 | article |
-----------------------------------------------
| 3 | 4 | 2 | article |
-----------------------------------------------
| 4 | 4 | 1 | article |
-----------------------------------------------
If i want to update article 1 in tag_hook table add two new tags (2,3 tag_id)
Help me thanks

Related

Create New Column In Mysql Database Row?

I have a database called form, in it a have a few columns.
+--------+----------+-----------+
| id | title | body |
+--------+----------+-----------+
| 1 | A | 10 |
+--------+----------+-----------+
| 1 | B | 3 |
+--------+----------+-----------+
| 2 | A | 9 |
+--------+----------+-----------+
| 2 | c | 40 |
Each row has an id representing a thread number. In the database when someone comments I have a php script that will pull the id from the database of the thread the user commented on and create a new column in that row within the database. Such as below:
+--------+----------+-----------+-------------+
| id | title | body | comment01 |
+--------+----------+-----------+-------------+
| 1 | A | 10 | zyx |
+--------+----------+-----------+-------------+
| 2 | B | 3 |
+--------+----------+-----------+
| 3 | A | 9 |
+--------+----------+-----------+
This way I will be able to extract the thread along with the comments and display them onto the page.
My question is how do I write the code to insert a new column in a thread id row when someone submits a new comment?

Doctrine orm one to many with reference key also containing strings

I have an entity called books and another one users.
Each book can have many authors(users).
So when I add a book I select the users that are registered on the site.
But if the author of the book doesn't have an account on the site I want to type his name by hand.
+----+---------+-----------+--+
| id | book_id | author_id | |
+----+---------+-----------+--+
| 1 | 1 | 1 | |
| 2 | 1 | Mr.Paul | |
+----+---------+-----------+--+
See how the author_id is a string but also an reference key to an user?
Can I do this with doctrine or I will have to manage the inserts myself?
Edit:I have no idea what the right title should be
Edit2:A possible solution will be to make another table containing only authors that don't have an account on the website.
I would recommend following approach
You have a table books a table accounts and the following:
authors_books
+----+---------+-----------+--+
| id | book_id | author_id | |
+----+---------+-----------+--+
| 1 | 1 | 1 | |
| 2 | 1 | 2 | |
+----+---------+-----------+--+
authors
+----+---------+------------+--+
| id | name | account_id | |
+----+---------+------------+--+
| 1 | Mr. Paul | 1 | |
| 2 | Simon | (empty) | |
+----+---------+-------------+--+
the account_id can be empty

Junction tables in MySQL

(Sorry, my english isn't very good)
Hi, I am trying to learn how to work with junction tables in MySQL and I can't figure how to do something. I know the basics of MySQL but I have never worked with "JOIN".
In this test project, I would like to be able to show on a page the app of a given category (you click on "Games", only the apps that are in the "Games" category will be displayed on the page). I would like to know what the SQL request should look like.
Second question, let's say that an App could fit 2 different categories, how can I manage to give that app 2 different Category_ID in my database ?
Here is what my Database looks like at the moment :
Table name: APPS
+------------+-------------------+
| App_ID (pk)| App_Name |
+------------+-------------------+
| 1 | Weather Network |
| 2 | Is it sunny 2.0 |
| 3 | The Weather App |
| 4 | Zelda |
| 5 | Megaman |
| 6 | Doom 3 |
+------------+-------------------+
Table name : CATEGORY
+-----------------+-----------------+
| Category_ID (pk)| Category_Name |
+-----------------+-----------------+
| 1 | Games |
| 2 | Weather |
+-----------------+-----------------+
Table name : JUNCTION_APP_CATEGORY
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+
For your first question, the answer is
SELECT a.*, c.*
FROM APPS a, CATEGORY c, JUNCTION_APP_CATEGORY ac
WHERE a.App_ID=ac.APP_ID
AND c.Category_ID=ac.Category_ID
AND ac.Category_ID=<category_id for category "Games">
For your second question, you can use both APP_ID and Categor_ID as the primary key of table JUNCTION_APP_CATEGORY(note NOT TWO pks, but use the two columns together as ONE pk). So that you can put data like this:
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 1 | <-- APP_ID=1 belongs to both cat 1 & 2
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+

Merge and sort two mysql tables with different columns

Table 1: user posts
+----+----------+-------------+--------+------+------+---------------------+
| id | parentID | commenterID | userID | post | tags | date |
+----+----------+-------------+--------+------+------+---------------------+
| 1 | NULL | 1 | 2 | hi | NULL | 2013-08-01 16:24:49 |
+----+----------+-------------+--------+------+------+---------------------+
Table 2: blog posts
+----+------+------+----------+-------+------+-------+----------+---------------------+
| id | type | tags | username | title | body | views | comments | date |
+----+------+------+----------+-------+------+-------+----------+---------------------+
| 1 | post | tag1 | user1 | lol | test | 0 | 0 | 2013-08-29 21:58:52 |
+----+------+------+----------+-------+------+-------+----------+---------------------+
I am trying to merge these two tables and sort them by date. None of these tables have anything in common so I am afraid I cannot use JOIN.
Table 1 represents "user posts" and table 2 represents "blog posts". I am building a "stream" of information such as it displays in the user's feed any updates/new posts coming in.
Any help will be greatly appreciated.
Thank you

MySQL: Select row from table which name is specified in a field of another table

I have a feed table with say fields:
id - unique feed id
created - the date the feed was created
table - the name of the table the rest of the feed info resides
Then I have say 2 tables: feed_image and feed_text. Now these 2 tables contain different information about a feed, different fields.
How is it possible (in MySQL) to extract the information for the feed from the appropriate table which name is specified in feed.table?
Here is how my schema looks like:
+------------------+
| table_a |
+---------------------+ |------------------|
| feed | | id |
|---------------------| +------+ feed_id |
| id <-------------------+-+ | field_in_a |
| created | | | ... |
| table | | | |
| | | | |
| | | | |
| | | +------------------+
+---------------------+ |
|
|
| +-------------------+
| | table_b |
| |-------------------|
| | id |
+--------+ feed_id |
| field_in_b |
| ... |
| |
| |
| |
| |
+-------------------+
Each feed exists either in table_a or table_b or table_c or ... (I have like 30 of them).
How can I specify which table to extract the info from (each table has a different structure).
Or, if I add indexes on each table_*.feed_id and map it to feed.id, would InnoDB do some magic, so when I JOIN them all it would look in just one of them, not all 30?
My latest idea is to have just one table feed with a field feed.content where I would store a serialized PHP object of a different PHP class representing the different feed type and its individual contents.
What is the best way to go regarding performance?
P.S.: No records would need to be selected / searched / ordered by individual parameters, just by created. The idea should be able to work well with 1 000 000+ records.
UPDATE:
To clarify about the 30+ table_a/b/c..
Each feed can be of too many different types (new ones will also be added with time):
An image feed would have VARCHAR(255) url field
A text feed would have LONGTEXT text field
A youtube.com feed would have VARCHAR(255) title, VARCHAR(255) video_id fields
A *.com feed would have * x1, * x2, * x3 ... fields
Each of these feeds will be then displayed with PHP according to type:
An image will be displayed as na image from the given URL
A text will be displayed as a pure text
A youtube.com feed would display a video player with the given title from the given video id
A *.com feed would display... :)
I would use a LEFT JOIN and alias my columns in the select and alias my tables in the join allowing you to return any and all information you need.
The with whatever language your pulling the results you can group and perform logic as necessary.
UPDATE:
Why do you have 30 tables exactly? Maybe one "meta" table with the feed creation date url it came from etc... and another table that contains a unique record id, feed id, content, content type.
That way you can join on one table where feed id's match as well as group by or filter by content type.
Visualization: Feed table
--------------------------------------------------------------
| feed_id | feed_name | feed_created | Feed_url |
--------------------------------------------------------------
| 1 | Feed 1 | 03/28/2012 | www.go.com |
--------------------------------------------------------------
| 2 | Feed 1 | 03/28/2012 | www.be.com |
--------------------------------------------------------------
| 3 | Feed 2 | 03/28/2012 | www.hi.com |
--------------------------------------------------------------
| 4 | Feed 3 | 03/28/2012 | www.ex.com |
--------------------------------------------------------------
Visualization: Feed Resources table
------------------------------------------------------------------------------------------------
| rec_id | feed_id | content | type |
------------------------------------------------------------------------------------------------
| 1 | 1 | 'hello world! | text |
--------------------------------------------------------------------------------------
| 2 | 3 | 'http://me.com/my-image | img |
------------------------------------------------------------------------------------------------
| 3 | 2 |{\'title\':\'VIDEO\',\'url\':\'http://me.com/1.mov\'}| vid |
------------------------------------------------------------------------------------------------
| 4 | 1 | 'Wow that was easy!' | text |
------------------------------------------------------------------------------------------------
Can't you do something like this:
+------------------+
| table_a |
+---------------------+ |------------------|
| feed | | id |
|---------------------| +------+ feed_id |
| id <-------------------+-+ | field_in_a |
| created | | | ... |
| | | | |
| | | | |
| | | | |
| | | +------------------+
+---------------------+ |
|
|
| +-------------------+
| | table_b |
| |-------------------|
| | id |
+--------+ feed_id |
| field_in_b |
| ... |
| |
| |
| |
| |
+-------------------+
And then join the records from table_a and table_b? MySQL is pretty efficient at that.
You should create a normalized layout as d_inevitable suggested.
You haven't told us exactly how you're displaying this data. But you can get a list of ALL feeds with select * from feed;
Then you can get additional data for the feed by searching the other tables. For your example of URLs, if table_a = URLs and field_in_a = URL
Whichever feed you're on, you'd search for URLs with the ID for that feed.
select * from URLs where feed_id = "id"
This would allow each feed to have 1 to many URLs associated with it.
You could do this for each type of data you'd have associated with a feed. The "feed_id" is your Foreign Key that you use to reference which feed it is.
The key is going to come down to how you're displaying this.
You're going to need to loop through all the Feeds, and then build a table (?) appropriately.
If a feed has two URLs, how do you want it to look?
Should it display
-------------------------------------------------
| Feed Name | Feed Created | URL |
-------------------------------------------------
| Feed 1 | 03/28/2012 | www.go.com |
-------------------------------------------------
| Feed 1 | 03/28/2012 | www.be.com |
-------------------------------------------------
| Feed 2 | 03/28/2012 | www.hi.com |
-------------------------------------------------
| Feed 3 | 03/28/2012 | |
-------------------------------------------------
or
-------------------------------------------------
| Feed Name | Feed Created | URL |
-------------------------------------------------
| Feed 1 | 03/28/2012 | www.go.com |
| | | www.be.com |
-------------------------------------------------
| Feed 2 | 03/28/2012 | www.hi.com |
-------------------------------------------------
| Feed 3 | 03/28/2012 | |
-------------------------------------------------
I think the data layout should be as d_inevitable suggested, and then you need to determine how you're going to display the data, and that will determine how you query it.

Categories