I wanted to update a table using many to many relations. I have three tables:
users;
badges;
user_badge;
In the users table I have: id, username and game_count. In the badges table I have: badge_id and badge_name. The user_badge table has id, user_id and badge_id.
The user_id and badge_id in the user_badge table represent the id columns in the users and badges tables. These are used as foreign keys in the user_badge table.
I would like to update the user_badge table with the values from the other two tables: users and badges.
For example, table Users, have the following details.
id:1 username:john game_count:1
Table badges has:
id:1 badge_name:firstbadge
So, when I apply a condition (e.g. game_count=1), the user_badge
table should update its column with user_id from users and badge_id from the badges table.
Can someone help me with writing this query?
Related
I have Two table in database one is employer and another is skill table. Skill table has all skills like type, language, and rate column and employer table has user contact fields. I want desire output which is in below image. the employ_id in skill table and id in employer table is foreign key relations.
I am a beginner with database designs. I have two tables shops and products with a many to many relation with a pivot table product_shop.
In the shop table, this how the structure looks like
shops table
id
name
user_id
products table
id
name
product_shop
product_id
shop_id
location
id
name
shop_id
Now my question is, i want to get all products belonging to a particular user or location, can i modify the products table to add (user_id & location_id) so as not to write complex queries.
Does it also foul the rules of database design?
I have a users table and a categories table which already contain data,
I also have a third table which called user_category which has three columns(id,user_id,category_id).
When the user first register he must choose a category or more than one from a dropdown. I want to insert the id of the user to user_id columns and the id of each category to the category_id so if the user choose more than one category he will have more than one record in the user_category table.
Use This
SELECT user_id FROM users
ORDER BY user_id DESC
LIMIT 1
This will give you the last user_id which has inserted Now by using this user id you can insert the category_id by for each loop or for loop
I have two tables in my database, users and user activities, The user table has id, first_name,last_name and other fields, the user activities table has, id, user_id (foreign key --> from the users table) and project id. I want to select distinct user names (combination of first and last name) where project_id is 1.
If I write this as a simple query it works fine, the query I wrote was:
SELECT DISTINCT users.first_name,users.last_name,`user_activities`.user_id FROM `users`
JOIN `user_activities`
ON users.id=`user_activities`.user_id
WHERE project_id=2;
How do i perform this query in YII?
There is two approaches.
Simple Query Builder
See this.
2) Relations to user table on user activities.
See this.
And at least, give it a try. And update, where you get stuck.
I have 2 tables. The parent table is learn_more and child table is reference_keys
Both tables are innodb
reference_keys has two columns:
key_id [index]
key_href
learn_more table
id [primary]
keys_id [foreign key]
page_title
page_content
What I am trying to do is get multiple links in the learn more table from the reference_keys table.
So example, learn_more table id:1, keys_id:1,3,4,8,13,25,..., page_title:Home Page: blah blah, page_content: blah blah......
The problem is that phpmyadmin will not allow me to put more than 1 id in the keys_id of learn_more.
//ERROR
//Warning: #1265 Data truncated for column 'keys_id' at row 1
I'm guessing the relation view is not setup correctly. - How do i fix this?
and on my page it shows the key_id in the echo instead of the value for the id: which is the key_href. so my page show "1" instead of the value for 1 which is a link..
Perhaps my sql query is not correct?
$SQL = "SELECT * FROM learn_more WHERE page_title = '$this_page'";
To build a many-to-many here is what you could do:
reference_keys has two columns:
key_id [index]
key_href
learn_more_to_reference_key
reference_key_id [FK to reference_keys]
learn_more_id [FK to learn_more]
learn_more table
id [primary]
page_title
page_content
Then you have essentially a 1:N on each side of the relationship. Notice that I removed the FK from the learn_more table, too.
So to grab the relationship you'd query like this:
SELECT * FROM Learn_More lm
INNER JOIN learn_more_to_reference_key lmtrk ON lm.id = lmtrk.learn_more_id
INNER JOIN reference_keys rk ON rk.id = lmtrk.reference_key_id
I believe the inner join is correct, i'm double-checking that.
If you want to have one row in learn_more correspond to multiple rows in reference_keys, you need to move the foreign key field from the learn_more table to the reference_keys table.
So instead of having a foreign key field in learn_more that points out to multiple rows of reference_keys (which, as you seem to be running into, is not supported), you have the multiple rows of reference_keys all point back to the learn_more table.
This would implement a one-to-many relationship between learn_more and reference_keys. If you need a many-to-many relationship (where each reference_key rows can be connected to many learn_more rows and vice-versa) you need to use a third table to establish a link between the two databases. See http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php for more information.
You're getting the keys_id because that is what is in the learn_more table. To get the key_href, you'll need to JOIN the learn_more table to the reference_keys table.
Also, #ametren is correct - you should have a many-to-many table that links your two current tables.
key_linking_table
id [primary and foreign key]
keys_id [primary and foreign key]
$SQL =
"SELECT lm.id, lm.page_title, lm.page_content, rk.key_href
FROM learn_more AS lm
LEFT JOIN key_linking_table AS klt
ON klt.id = rk.id
LEFT JOIN reference_keys AS rk
ON klt.key_id = rk.key_id
WHERE [condition]"