Create Contacts Database Which Refers to Users Without Duplicates - mysql

My question is similar (but at the same time completely different) than this question:
Contacts Database
The question is simple: How can I create a Contacts database table which stores a user id and contact id without duplicating keys.
For example, if I have a table called Contacts, it would have a column user_id, and a column contact_id.
Once I do that, it should be as simple as inserting the user and the added contact. Once that is done though, how do I select all of a user's contacts? Also, how do I narrow down the contact entry enough to delete it if need be?

I ended up just creating a table with two foreign keys and then selecting them based on either of the fields.
For example (pseudo code--no specific language, just english):
Table Contact:
user = ForeignKey(from user table)
contact = ForeignKey(from user table)
Then whenever I need something from them, I'll check if the user field contains what I want and then I'll check if the contact field has what I want. This way I don't have to repeat records and I can still find what I need.
Thanks for your answers.

Similar to the question in the link. You would have 3 tables.
Table 1
User_ID
Name
PK(User_ID)
Table 2
Contact_id
Address
Phone_Number
etc...
PK(Contact_id)
Table 3
User_ID
Contact_id
PK(User_ID, Contact_id)
Here you would have ContactID in table 2 as an autoinc column.
Also, when inserting in Table 3, MySQL would throw an error if there is a duplicate.
To select all of a users contacts, use:
SELECT *
FROM Table_2 join Table_3
ON Table_2.Contact_id = Table_3.contact_id
WHERE Table2.User_id = <userid>
Or if you need it for a particular name, then
SELECT *
FROM Table_1 JOIN Table_2
ON Table_1.User_id = Table_2.User_id
JOIN Table_3
ON Table_2.Contact_id = Table_3.contact_id
WHERE Table1.name = <user name>

there are two questions.
" how do I select all of a user's contacts?"
So you have a table tbl_contacts(user_id, contact_id) both them are your primary key, so you won't get duplicated data.
I you want to list all contacts for user_id = ?
SELECT *
FROM tbl_contacts
WHERE user_id = ?
You might want to clarify your second question "Also, how do I narrow down the contact entry enough to delete it if need be?"
You probably have some other properties belong to the user's contact and you will need to use those properties to search for.(eg.: contact_name or contact_number) and when you have 1 record as a result of a query you can -> DELETE FROM tbl_contact WHERE contact_id = ?
If this is not the answer you wanted please clarify your question.

Related

MySQL query to find values in table with given value and find others that are similar

I have a table called followers and I want to be able to find the current users followers, display that list, and then compare those values against another user ID to see if they are following them or not from that same table
This is the table I'm using currently to get the list of followers for a specific user:
followers
-------
followId - primary key, the unique id for the follow relationship
userId - the user that is following someone
orgId - that someone that the user is following
I tried using a union query but I wouldn't want to due to performance reasons (table may contain high volume of records) and because it isn't scalable (I think)?
The expected output should be the list of orgId's for which the user(the user I am checking against) is following, and another column that shows whether my user(my userId that I provide) is following that orgId value (i.e a following column).
Hmmm, if I understand correctly, you have two users and you want to know which orgs followed by the first are also followed by the second:
select f.orgid,
(exists (select 1
from followers f2
where f2.userId = $seconduserid and
f2.orgid = f.orgid
)
) as seconduserflag
from followers f
where f.userId = $firstuserid

How to update a MySQL table field where 2 columns match (email) but another set of columns (member ID) are not synchronized

I've searched through a ton of these Stack Overflow questions and I'm having trouble getting sorted on this. The biggest problem I have is the queries I've found and tried are timing out (20,000 records in the member table and the wp_users table). I'm not super advanced with MySQL so I'm sure there must be a way to do it properly but I haven't yet figured it out.
I have a wordpress user table and a very dated member plugin table.
wp_users: ID, user_email
members: member_id, email
I'm trying to compare the ID, user_email field to this member plugin table that has member_id, email. The email in both tables should match but the ID / member_id may not.
My goal is to perform an UPDATE to change the member_id of all users in that member table to match the ID in the wp_users table where the emails match between both tables, but most everything I've found and tried times out.
Here is an example of a select query I tried to use to update, but like a lot of the other things I tried it just times out after 600 seconds.
UPDATE IGNORE members_tbl AS eMember JOIN wp_users AS u ON eMember.email = u.user_email SET eMember.member_id = u.ID WHERE eMember.member_id != u.ID
The query you have should work just fine. The problem is likely that you do not have an index on these tables on the email. There is no getting around doing the join on that email, but joins on non-indexed columns are supremely inefficient. I would add an index on the email column on both tables.

What sql should be used for inventory tables

I have a MySQL db set up like so,
user_table table
user_ID PK
username
password
email
level
location
game_items table
game_item_is PK
item_name
user_inventory table
user_inventory_id PK
user_id FK
game_item_id FK
quantity
This is a db for a game i am building and i am trying to code an sql query to get the following data out of the db:
Level
Location
A list of game items the user has associated to their user_id
The data i already have is the user_id and username.
So how would i go about creating this sql statement? would i use joins? if so where?
I want to get every game_item the user has associated with their account so would this include * anywhere?
Thanks
Ok so after a while playing with the sql on phpmyadmin i got the result i wanted, if anyone would like to know here is the code:
SELECT ut.username, ut.level, ut.location, ui.quantity, ui.game_item_id
FROM user_table ut, user_inventory ui
WHERE ut.user_id = ui.user_id
AND ut.user_id =1
If I understand your question correctly, just select the level and location in one query and the inventory items in another
SELECT level, location FROM user_table WHERE id = ..
SELECT * FROM user_inventory WHERE id = ..

Return records from one table where Field not found in another table

I'm so lost here I don't even know how to best title my question.
I am creating a simple dating site. I want the women to be able to block the men just like all other dating sites. When this happens, I don't want the womens' profiles to be returned in a query.
Table A
Members table containing all the profile information including a member name
Table B
Blocked members table containing the woman's name and the man's name for each case in which the woman has blocked a man
So, I want something like this:
$query = Return all records from table A where sex=female and there is no record in table B containing the woman's name and the man's name
I thought I would run a query against table B to retrieve all women who have blocked me, then run a query against table A to return all females in which the woman's username is NOT contained in the results of my first query. However, I can't figure out how to do this.
If I understand your question...seems like a simple join, no? Not sure if I'm misunderstanding. Something like this perhaps:
SELECT * FROM Table1 WHERE Table1.ID NOT IN (SELECT BLOCK_ID FROM table2)
So Table1 has all ID's of the women, and Table2 has all block id's (for example) and you want what is not in that? Obviously some changes required on top of this.
If you wanted to see a list of all the female members who had blocked the current user, you would use a query like:
SELECT member.*
FROM TableA member
JOIN TableB blocked ON (member.name = blocked.user_who_blocked)
WHERE member.sex = female
AND blocked.blocked_user_name = 'Joe McCurrentUser'
;
So, if you want to see the set of users where that is not the case, you use a LEFT JOIN and look for a null id.
SELECT member.*
FROM TableA member
LEFT JOIN TableB blocked ON (member.name = blocked.user_who_blocked)
WHERE member.sex = female
AND blocked.blocked_user_name = 'Joe McCurrentUser'
AND blocked.id IS NULL
;
You can modify as you wish to use the actual columns in your tables. Make sure you have indices on both the user_who_blocked and blocked_user_name columns in that table.
Would this work?
Select * from Table A
inner join Table B on a.womans_name = B.womans_name and B.mans_name="Mans Name"
where B.womans_name IS NULL
If Table B contains a record with the matching womans_name and mans_name then the join will create one record containing all the fields in Table A and Table B but the Where clause will reject this record because the womans_name from Table B will not be null. If Table B does not contain a matching record then all those fields will be null (including B.womans_name) so the Where clause is satisfied.

How do I select a record from one table in a mySQL database, based on the existence of data in a second?

Please forgive my ignorance here. SQL is decidedly one of the biggest "gaps" in my education that I'm working on correcting, come October. Here's the scenario:
I have two tables in a DB that I need to access certain data from. One is users, and the other is conversation_log. The basic structure is outlined below:
users:
id (INT)
name (TXT)
conversation_log
userid (INT) // same value as id in users - actually the only field in this table I want to check
input (TXT)
response (TXT)
(note that I'm only listing the structure for the fields that are {or could be} relevant to the current challenge)
What I want to do is return a list of names from the users table that have at least one record in the conversation_log table. Currently, I'm doing this with two separate SQL statements, with the one that checks for records in conversation_log being called hundreds, if not thousands of times, once for each userid, just to see if records exist for that id.
Currently, the two SQL statements are as follows:
select id from users where 1; (gets the list of userid values for the next query)
select id from conversation_log where userid = $userId limit 1; (checks for existing records)
Right now I have 4,000+ users listed in the users table. I'm sure that you can imagine just how long this method takes. I know there's an easier, more efficient way to do this, but being self-taught, this is something that I have yet to learn. Any help would be greatly appreciated.
You have to do what is called a 'Join'. This, um, joins the rows of two tables together based on values they have in common.
See if this makes sense to you:
SELECT DISTINCT users.name
FROM users JOIN conversation_log ON users.id = converation_log.userid
Now JOIN by itself is an "inner join", which means that it will only return rows that both tables have in common. In other words, if a specific conversation_log.userid doesn't exist, it won't return any part of the row, user or conversation log, for that userid.
Also, +1 for having a clearly worded question : )
EDIT: I added a "DISTINCT", which means to filter out all of the duplicates. If a user appeared in more than one conversation_log row, and you didn't have DISTINCT, you would get the user's name more than once. This is because JOIN does a cartesian product, or does every possible combination of rows from each table that match your JOIN ON criteria.
Something like this:
SELECT *
FROM users
WHERE EXISTS (
SELECT *
FROM conversation_log
WHERE users.id = conversation_log.userid
)
In plain English: select every row from users, such that there is at least one row from conversation_log with the matching userid.
What you need to read is JOIN syntax.
SELECT count(*), users.name
FROM users left join conversion_log on users.id = conversation_log.userid
Group by users.name
You could add at the end if you wanted
HAVING count(*) > 0