I have 3 three tables as follows a user can have many contacts and a contact can be listed by many users...
//user table
user_id | username|password|fname|lname|email|contactnumber
//contact table
contact_id | fname | lname |email|contactnumber
//user_contact table
user_id |contact id | relationship type |relationship state
My query must display all the contacts that is link to the selected user ...any advise will be helpful
so it will look like this
Result:
user fname | user lname | email address | contact number of user | contact first name | contact last name | relationship type | relationship state
Correct me if I understand your question wrong:
so the user table and contact table has a many to many relation?
then you can do
select u.fname,u.lname,u.email,u.contactnumber,c.fname,c.lname,uc.relationship_type,uc.relationship_state
from user as u
inner join user_contact as uc on u.user_id=uc.user_id
inner join contact as c on uc.contact_id=c.contact_id
where u.user_id=<userId>
Related
I have a mysql database with the following tables.
Excursion Table
Excursion ID | Port ID | Excursion Name
Bookings Table
Booking ID | User ID | Excursion ID | Number of People | Event Date
I want to be able to create a view, like the diagram below when given a certain UserID from the bookings table.
Booking ID | Excursion Name | Excursion ID | Number of People
Simply try this:
CREATE VIEW Your_View_Name AS
SELECT BT.Booking_ID, ET.Excursion_Name, ET.Excursion_ID,BT.Number_of_People
FROM Excursion_Table ET
JOIN Bookings_Table BT ON ET.Excursion_ID = BT.Excursion_ID
WHERE BT.User ID = ????;
CREATE VIEW `yourDB`.`yourViewName` AS
SELECT
`a`.`BookingID` AS `BookingID`,
`b`.`ExcursionName` AS `ExcursionName`,
`b`.`ExcursionID` AS `ExcursionID`,
`a`.`NumberOfPeople` AS `NumberOfPeople`
FROM
(`yourDB`.`BookingsTable` `a`
JOIN `yourDB`.`ExcursionsTable` `b`)
WHERE
(`a`.`UserID` = 'YourValue')
Hope this solves your issue.
I have 3 type of user roles: Patient, Doctor, Pharmasist
And Tables:
Users
id | name | surname | username | password | etc..
Roles
id | name
Patient
Doctor
Pharmasist
users_roles
id | user_id | role_id
And I want to implement tables such as: doctor_info, patient_info, pharmasist_info.
For example:
doctor_info
id | experience | qualification | user_id
What relationship should I use to tie users with doctor_info, patient_info, pharmasist_info and how to implement it correctly?
Assuming all id column from each table is primary key.
users:
id, name, ...
roles:
id, name
users_roles:
id, user_id, role_id (make user_id UNIQUE key, so 1 user can only have 1 role)
doctor_info:
id, user_id, ... (make user_id UNIQUE key as well, so 1 user can only be 1 doctor)
patient_info:
similar to doctor_info
pharmasist_info:
similar to doctor_info
(OPTIONAL) If 1 user has 1 role and 1 role is belonged to many users, you could remove users_roles table completely, and just add role_id in users table.
One issue is that you can have: 1 user can be a doctor, a patient and a pharmasist altogether with your table structure. You'll have to add some validation in your code to make sure it won't happen.
I have two tables in my problem, one is the USER table with all details of user and a REQUEST table with friend requests from all users to all users. I want to list all the friends of one user who logs into my application.
The USER table attributes are,
id | uname | passwd | fname | e mail | dob | mobileno | habbits | tastes | profession | image
11 | nagaraj | ***** | naga | ng#gml | 3/94 | 998345 | singing | sports | teacher
12 | chiraag | ******* | chiru | ch#gml | 2/93 | 894617 | writing | music | student
Similarly, the REQUEST table attributes are,
rqstTO | rqstFM | fname | e mail | mobile | status
chirag | nagaraj | nagaraj | ng#gml | 99821 | accepted
The tables look like this. Now I want to display the details of the friends( from USER table), only those friends who have accepted the request from the user who logged in. Output may be multiple users. The conditions to be satisfied are
all details of friends (from user table)
rqstFM should be the user logged in and rqstTO must be the friends to whom he sent requests (since request table has request details from all to all users)
status=accepted (they must have accepted the request)
The query I have written is
SELECT *
FROM user
WHERE habits='singing' AND uname = (
SELECT rqstTO
FROM request
WHERE rqstFM ='"+username+"' AND status='Accepted'
);
(here +username+ is the uname of the person logged in obtained from HTML FORM tag). The error I keep getting is "subquery returns more than one row" but I do need multiple rows as logged in user may have sent requests to many ppl and among those all who accepted his request, their details need to be displayed from user table. Please, correct my query or provide an appropriate query for my problem
You need to use IN instead of =:
You can use as mentioned below:
SELECT * FROM user
WHERE habits='singing' AND uname IN
( SELECT rqstTO FROM request WHERE rqstFM ='"+username+"' AND status='Accepted');
However using subqueries are not recommended due to performance issues. You should rewrite your query by using JOIN
You can use 'JOIN' like below: (I haven`t tested it)
SELECT u.* FROM user u
JOIN request r ON u.uname = r.rqstTO
WHERE u.habits='singing' AND r.rqstFM ='"+username+"' AND r.status='Accepted';
You want something like this (not tested it however):
SELECT user.uname, request.fname
FROM user
INNER JOIN request ON request.fname = user.fname
WHERE request.status = 'Accepted';
However, I would probably have a bit of a rethink of those tables, maybe this is personal preference but you would likely be better off having a join table that simply contains an id and the username, and then join on two other tables. One containing the requests and one containing the member details.
I apologise in advance if this might seem simple as my assignment needs to be passed in 3 hours time and I don't have enough time to do some further research as I have another pending assignment to be submitted tonight. I only know the basic MYSQL commands and not these types. Please help.
Say that I have two tables:
________________ _________________
| customers | | agents |
|________________| |_________________|
|(pk)customer id | |(pk) agent_id |
|(fk) agent_id | | first_name |
| first_name | | last_name |
| last_name | | address |
|________________| |_________________|
Basically I would just like to know how to query something like: (in incorrect terms)
SELECT * FROM customers WHERE agent_id = '(agent_id of Michael Smith from the AGENTS table)'
obviously I only have the agent_id of the agent and i can directly call it if i know what the agents name is based on the id like:
SELECT * FROM customers WHERE agent_id = '4'
but how can i query it by submitting the agent first name and last name as parameter?
(first name and last name because agents can have the same names, or even same last names)
Remember your foreign key does not help you building the query, you have to tell the database what you want in the query (however, a foreign key can help data spread across tables more consistent).
You can use a JOIN here.
You can implement it like this:
SELECT *
FROM customers C
INNER JOIN agents A ON C.agent_id = A.agent_id
WHERE A.last_name = 'Smith'
AND A.first_name = 'Michael';
You can do it without a join as well.
select *
from customers
where customers.agent_id in (
select agents.agent_id
from agents
where agents.first_name = 'Michael' and agents.last_name = 'Smith'
);
My system uses a table of users with several elements including their online status. Now I would like to add a feature so each user can add a contact i.e an existing user from the database into their list of friends or contacts showing their name and online status. However I need any changes in the online status to updated on the contact list as they happen so creating a new table wouldn't help.
I have been looking into views for this but don't have too much experience with databases to I would like to know if this is the correct way of going about it and a bit more detail on how to do it.
Here are the steps I was thinking of:
When users registers, create a view i.e view_name = username_view.
To add a contact select data from main users table and add to user's view
To delete a contact delete selected data from view.
I am not sure if this is possible with views so if it isn't can some please help me out.
Thanks.
ER databases = Entites and Relations databases handle that by have one table whit the entites "users", and one table for the relationship "friends" that connect a user to another user.
a query for that can be:
SELECT users.*
FROM friends
LEFT JOIN users ON (users.user_id = friends.friend_id)
WHERE friends.user_id = :user
Exemple:
a user table, for the entity user
CREATE TABLE user (user_id SERIAL, name TINYTEXT NOT NULL);
exemple users
user_id | name
1 | Anna
2 | Bertil
3 | Carl
4 | David
5 | Erik
a friends tabel, for relations between two users, (not one per user, just one)
CREATE TABLE friends (
user_id BIGINT UNSIGNED NOT NULL,
friend_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (user_id, friend_id)
);
if Anna adds Beril and Carl as friends, and Carl adds David and Erik as friends, the content of the table going to be:
user_id | friend_id
1 | 2
1 | 3
3 | 4
3 | 5
if we want to list the names of Annas friends, and we allredy know that Annas user_id = 1, then we can use this query (like the one above)
SELECT users.name
FROM friends
LEFT JOIN users ON (users.user_id = friends.friend_id)
WHERE friends.user_id = 1