I have two table one is accounts table and account group table. currently I am using the auto increment id as foreign key in the accounts table but my question is I have multiple tables where it will join with accounts table and I need to retrieve account group name in a view where there is 20 union all queries which it join to the accounts table.
SELECT acc.name,accgrp.groupName,bill.supplier,bill.amount
FROM bills bill
INNER JOIN accounts acc ON acc.accId=bills.accId
INNER JOIN accountGroup accgrp ON accgrp.groupId=acc.groupId
UNION ALL
.....
What is the correct way to save the data in accounts table saving id is the best approach or saving the group name is best approach. If i used id as foreign key and join in 20 union all is it make my query slower? or saving group name in my accounts table and retrieve it without join make my query faster?
Related
I have 4 tables:
Student(id(PK),name,subject_enrolled(FK)
Teacher(id(PK),name,subject_teaches(FK)
Subject(subject_code(PK),subject_name)
Combined(FIELDS not yet known)
By using MYSQL triggers, I want to insert data to the student table, it will automatically get the names of the teacher table and place it to the Combined table based on the subject enrolled and the subject teaches. What is the best approach to realize that ?
you can get it USING following query you don't need separate table and triggers
SELECT ST.name as Student_name,T.name as Teacher_name ,S.subject as Subject ,S.subject_code as Subject_code from Student ST
inner join Subject S on S.subject_code=ST.subject_enrolled
inner join Teacher T on T.subject_teaches=S.subject_code
where ST.id=<your student id>
I have 2 tables and need to make a View table
These are 2 tables that I have:
user table with some fields: id, username, email, pass
system_log table with 2 fields: uid, message (uid means user_id)
Now, I'd like to have a view table which gives me a table with 2 fields, username and message.
Here is my problem: I need username from user table in my view table while I have uid in my system_log table. Basically, instead of having uid, I need the username
The project and list of fields in the actual project are more than these but I just made it simple here to make my points clear.
You can use inner join to get the data from both tables
CREATE VIEW `view_name` AS
SELECT
u.user_name,
s.message
FROM users u
INNER JOIN system_log s ON u.id = s.user_id;
CREATE VIEW my_view AS
SELECT u.username, s.message FROM
user u INNER JOIN system_log s
ON u.id = s.user_id
A subset of my database contains 3 tables
users
user_details
tasks
The main column of the user table is:
id {PK}
The user details table contains information about the users (only some users have these details, students). Thus the main columns of the users details table are:
user_id{PK,FK} | supervisor_id {FK}
The supervisor_id is an id in the users table. Each student has one supervisor.
Lastly, there is the tasks table where only students create tasks and the main columns of the tasks table are:
task_id{PK} | user_id{FK}
The problem I am having is getting a proper query for, if a supervisor wants to see all his students tasks. I know you can query all the students in the user_details table who have the supervisor's id. Then create another query where you select all the tasks whose user_id matches that of the first query performed.
This does not seem like a very efficient was to go about achieving this result. Are there better alternatives?
select ud.supervisor_id, ud.user_id, t.task_id
from user_details ud, users u
where ud.user_id = t.user_id
What you are looking for is a join. Instead of writing two separate queries to get the information, a join will allow you to connect the tables that you have in one query and get the information you need much faster.
Select *
From user_details ud
join tasks t
on ud.user_id = t.user_id
Where ud.supervisor_id = ?
The join essentially allows you to create one big table out of all of the columns of the tables you are using. The on keyword tells sql which values go together, so that you know all of the tasks belong to the student whose id matches the id that the supervisor has. Then, you can select whatever columns you like out of either table (as well as a lot of other fancy things).
How to show the common data from all table on base of vendorID where vendorID is unique key in my vendor table. and i use this as an foriegn key for all other(cost table, hour table,employee table, temporary table and ) table.
This is my Cost table struructre
This is my Hour table structure
This is my Temporarytable structure
This is my Employee table
This is my Final table vendor there are vendorID is unique Key
And i have use the following query but it is showing the different data.
SELECT * FROM cw_employee_csv as emp
inner join cw_cost_hour as cost on cost.vendorid=emp.vendorid
inner join cw_temp_employee as temp on cost.vendorid=temp.vendorid
inner join cw_hour_company as hour on temp.vendorid=hour.vendorid
inner join cw_vendor as vendor on temp.vendorid=vendor.vendorid
where vendor.companyid=1 ORDER BY hour.timeFrom ASC
If I understand, try this:
In your query in the field list explicit fields you want to show, for example:
SELECT
emp.assignmentid, cost.bill_type
FROM
cw_employee_csv AS emp
INNER JOIN
cw_cost_hour AS cost ON cost.vendorid = emp.vendorid
INNER JOIN
cw_temp_employee AS temp ON cost.vendorid = temp.vendorid
INNER JOIN
cw_hour_company AS hour ON temp.vendorid = hour.vendorid
INNER JOIN
cw_vendor AS vendor ON temp.vendorid = vendor.vendorid
WHERE
vendor.companyid = 1
ORDER BY
hour.timeFrom ASC
If the cardinality of tables is different and you want to show only one row per vendor, you must write a main query with cw_vendor and use some subquery to retrieve additional information by other table. Obviously, about parent table of your vendor table you can use INNER JOIN information without use subqueries.
EDIT AFTER COMMENT:
You start by this query (after chat discussion):
I extract an aggregate by company (I suppose to summarize your cost by vendor, if you want to apply another formula, it's the same algorithm)
select
sum(t.vendor_cost), t.companyid
from
(select
vendor.companyid as companyid, vendor.id as vendorid,
(select SUM(c.cost)
from cw_cost_hoyr c
where c.vendorid = vendor.id) as vendor_cost
from
cw_vendor vendor) as t
group by
t.companyid
Now we add other information but I must understand what do you want exactly with their relations
hello every one please help me regarding how to make a good query to fetch the data from three table. I am making a database in which there will be users , employee and services which will provided to users via employee so i have set that table name as task. Now i can add the task by fetching the data from different table , the thing right now i want to do is show the entire record by fetching all the client data and services available to him and and who will be going to perform that services . so how can i put the query on the task table to fetch the data from the other table. Thanks
**task table**
task_id user_id employee_id service_id starttime endtime date
by using this table i have to fetch data from other three table by using
user_id employee_id service_id
employee table
employee_id pno name age pic_path
user table
user_id pno name age pic_path
service table
service_id name description
SELECT
t.*,
e.name AS employee_name,
u.name AS user_name,
s.name AS service_name, s.description
FROM tasks AS t
INNER JOIN employees AS e ON e.employee_id = t.employee_id
INNER JOIN users AS u ON u.user_id = t.user_id
INNER JOIN services AS s ON s.service_id = t.service_id