I need a help with a PHP/MySQL issue. I have a table named users and other named relationships.
users
--------------
id (PK)
name
email
etc
relashionships
--------------
id (PK)
id_user (FK to users.id)
id_friend (FK to users.id)
rating
I'm trying to INSERT multiple relationships but I don't want duplicated entries. I want to ignore the current row if the row is duplicated. I can't use the IGNORE statement because the id_user and the id_friend columns aren't unique. A user/friend may have multiple relationship rows.
Any tip?
You can create a unique key on the id_user/id_friend tuple. Neither of them are unique, but their combination is.
See multiple column indexes on the documentation.
Thanks amenadiel, I found that solution here and worked for me!
CREATE UNIQUE INDEX relation ON relationship (id_user, id_friend)
Related
it's late, my child cried all dayand I'm expected to pull this off by the next 3 hours.
I have a 3 MySQL tables "entities", "users", "priviledges" (all have their id as key).
The table "priviledges" is having these columns: id(PRIMARY KEY), user_id, entity_id, priviledge
The thing is that when...
I grant the highest priviledge(3) to a user the system should create new table entries for all remaining entities
(once you become "admin" for one entity, you should be "admin" for all entities; if you should have priviledge(2) for some entity already, it should be changed to 3).
I cannot alter the SQL structure and I'm using PHP with PDO. I tried, but I always end up with duplicate entries in "priviledges" as I'm not able to utilise INSERT IGNORE (yep, still SQL noob).
Right now I just select all users with priviledges.priviledge(3), then I select all DISTINCT entities.id and dump them into PHP arrays and then I loop foreach through every "user" and "entity". So instead of sending 1 query, I'm ending with USERSxENTITIES and rightnow its over 500 queries.
Will there be a merciful soul to help me?
First it seems from your data structure that (user_id, entity_id) is meant to be unique.
Because of your "no table schema changes" criteria (otherwise I'd drop id (assumed to be an auto_increment) and add this as the primary key), so instead, create a unique index:
CREATE UNIQUE INDEX uniq_u_e ON priviledges (user_id, entity_id)
You probably have duplicate already so search existing questions for deleting duplicates and then add this index.
I assume you have another table of entities.
To create an admin (3) user on all entities that has an id as its entity primary key.
INSERT INTO priviledges( user_id, entity_id, priviledge)
SELECT 42 as user_id, id, 3
FROM entities
ON DUPLICATE KEY
UPDATE priviledge = 3
So this ensure that the user 42 has admin(3) access on all entities.
I have 2 tables in database user_info and product_info.I save user_id,name,email,etc in user_info and product_id,product_name,product_description in product info.Now I have to look for a particular product so how do I get which product was uploaded by whom.How to link user_info with product_info in Android
For this, you have two approaches
you need to create a pivot table having three fields as table user_product_info with fields: id , user_id and product_id. You can set user_id and product_id which is related. here id is a primary key which you can use for further things.
or you can create a field user_id in product_info table and can use that.
Hope it helps you.
I have the mysql tables
Contract
-**contract_id** (PRIMARY)
Contract_to_groups
-**group_id** (PRIMARY)
-contract_id
Groups_to_users
-group_id
-user_id
I have this multiselect(multiselect.js) where elem is the users
I have already the contract and I want to create a group of users. So I select my users from multiselectand then click submit. On submit in my table Groups_to_users should be saved the users I selected from the multiselect.
I have the problem that if I set in Contract_to_groups the group_id as AUTO_INCREMENT I cannot save in Groups_to_users the users for this group because I dont know the group_id of Contract_to_groups, it is AUTO_INCREMENT and especially if I have four(4) multiselects and each with users how can I save in these two(2) tables Contract_to_groups, Groups_to_users the data with the correct ids ?
Try to modify your Contract_to_groups table, and add an id which wil be the auto incrementing primary key:
-**id** (PRIMARY)
-group_id
-contract_id
I have created two separate tables.
Table 1 (username,email,gender,age)
Table 2(taskname,description,createdon,duedate,workstatus).
Now I want to create a
table 3 (taskname,description,createdon,duedate,staffs,workstatus)
where staffs will show all the registered members' name and admin can select multiple users to allocate in a task.
I am a premature php learner.
Will anyone help me out to solve the problem,please ?
First of all, create two primary keys as user_id and task_id in table 1 and table 2 respectively. Once you are done with it, create table 3 with fields user_id and task_id as two foreign keys referencing table 1 and table 2 respectively and your requirement should get fulfilled.
table
ID|owner_id|work_id|lorem|etc|
1 |00123 | 00213 |XXXXX|XXX|
2 |00124 | 00213 |XXXXX|XXX|
owner_id (fk) owners.id (owners[id,name,etc])
work_id (fk) work.id (work[id,name,etc])
question is can I set codeigniter that when I
select(table.*,work.name as work,owners.name as owner) from table
it automatically handle joins since that table already contain the fk-ref ? or I must include join('owner','owner.id=table.owner_id) ?
actually all what I want is that when I select a table that contains a fk this fk column is replaced with one column from ref row by just passing the column name on ref table without having to worry about creating a specific function in my module for that each query.
My current solution:
was to create a view for each table that contain a relation and replace all fk columns with desired ref value, but since i have 6 tables 5 of them with fk,i now have 6 tables and 5 view (11 tables)in db which is really kind confusing for me, so any smarter way to do this ?
I think you are making some confusion on what FK is and what it does within a table.
FK constraint grants data integrity when it's present and relates data within tables. It doesn't join anything.
If you want to select records across related tables, you either use a
SELECT * FROM table1,table2 WHERE table1.K1 = table2.FK1
or
SELECT * FROM table1 JOIN table2 ON table1.K1 = table2.FK1
AND YES, you need to tell CodeIgniter to do those queries