UPDATED:
I am using MySQL statement to create a view:
I need to show Editors First Name, Last Name and the City if they shipped more than 50 books. The three tables I have are:
create table editors (
ed_id char(11),
ed_lname varchar(20),
ed_fname varchar(20),
ed_pos varchar(12),
phone varchar(10),
address varchar(30),
city varchar(20),
state char(2),
zip char(5),
ed_boss char(11));
create table titleditors (
ed_id char(11),
title_id char(6),
ed_ord integer);
create table salesdetails (
sonum integer,
qty_ordered integer,
qty_shipped integer,
title_id char(6),
date_shipped date);
Can anyone tell me what code would be to create this result?
I didn't make the tables, I just have to work with what I was given.
Antiquated syntax (note the intermixing of join conditions and filter conditions):
CREATE VIEW qtyorderedview AS
SELECT
salesdetails.title_id, salesdetails.qty_shipped,
editors.ed_id, editors.ed_lname, editors.ed_fname, editors.city
FROM
titleditors, salesdetails, editors
WHERE
titleditors.title_id = salesdetails.title_id
AND editors.ed_id = titleditors.ed_id
AND salesdetails.qty_ordered > 50
Modern syntax (join conditions and filter conditions are separate):
CREATE VIEW qtyorderedview AS
SELECT
salesdetails.title_id, salesdetails.qty_shipped,
editors.ed_id, editors.ed_lname, editors.ed_fname, editors.city
FROM
titleditors
INNER JOIN salesdetails ON titleditors.title_id = salesdetails.title_id
INNER JOIN editors ON editors.ed_id = titleditors.ed_id
WHERE
salesdetails.qty_ordered > 50
Joins against views work exactly like joins against tables. Just use the view name in place of a regular table name.
SELECT e.*
FROM (
SELECT DISTINCT te.ed_id
FROM (
SELECT title_id
FROM sales_details
GROUP BY
title_id
HAVING SUM(qty_shipped) > 50
) t
JOIN titleditors te
ON te.title_id = t.title_id
) te
JOIN editors e
ON e.ed_id = te.ed_id
Related
I've a view and a table. I'm inserting data into table on the basis of that view. I want that view to eliminate same data that is already inserted into table.
This is the view which is named view_1.
CREATE VIEW view_1 AS
SELECT
y.employee_registration_id,
x.employee_first_name,
x.employee_last_name,
x.employee_email,
y.employee_current_salary_amount,
(SELECT
x.salary_of_year_month_YearMonth
FROM
salary_of_years_months x
LEFT JOIN
status_salary_of_years_months y ON x.salary_of_year_month_id = y.salary_of_year_month_id
WHERE
y.year_month_status = 'Ongoing') AS salary_of_year_month_YearMonth,
(SELECT
CONCAT(MONTHNAME(x.salary_of_year_month_YearMonth),
'-',
YEAR(x.salary_of_year_month_YearMonth))
FROM
salary_of_years_months x
LEFT JOIN
status_salary_of_years_months y ON x.salary_of_year_month_id = y.salary_of_year_month_id
WHERE
y.year_month_status = 'Ongoing') AS salary_of_year_month_YearMonth_MonthYear
FROM
basic_info_employees x
LEFT JOIN
current_salary_employees y ON x.employee_registration_id = y.employee_registration_id
WHERE
y.employee_current_salary_amount > 0;
This is the table.
CREATE TABLE `salary_tracker_employees` (
serial_no INT(4) NOT NULL AUTO_INCREMENT,
employee_registration_id INT(4),
employee_first_name VARCHAR(30),
employee_last_name VARCHAR(30),
employee_email VARCHAR(30),
salary_of_year_month_YearMonth VARCHAR(50),
employee_current_salary_amount DOUBLE(8 , 2 ),
PRIMARY KEY (serial_no),
UNIQUE (employee_registration_id , salary_of_year_month_YearMonth)
);
I want to get a view that will intersect the between view_1 and salary_tracker_employees.
Is there any way we can reuse the reference of table we joined in a sub query?
I have three tables:
task_categories, information about task categories
task_priorities, priorities associated with task categories
task_links, url links for each individual tasks.
Please check this SQL Fiddle.
CREATE TABLE task_categories (
task_category_id int,
code varchar(255),
name varchar(255)
);
CREATE TABLE task_priorities (
priority_id int,
task_category_id int,
priority int
);
CREATE TABLE task_links (
task_links_id int,
task_category_id int,
title varchar(255),
link varchar(255),
position int
);
We'd need to join all these tables if we need links of tasks that has high priority. Something like this
select * from task_links t_links
inner join task t on t_links.task_id = t.task_id
inner join task_priorities t_priorities on t.task_id = t_priorities.task_id
where t.code in ('TASK_P2', 'TASK_P3') and
t_priorities.priority = (select min(priority) from task_priorities tp
inner join task t on tp.task_id = t.task_id
where t.code in('TASK_P2', 'TASK_P3'))
order by t_links.position;
Is there any way to optimize this query? This query has joined table twice, I think there should be a better way to write this query.
The logic for your subquery is incorrect. It is not selecting the minimum priority for each task.
I am guessing that you really want:
where t.code in ('TASK_P2', 'TASK_P3') and
tp.priority = (select min(tp2.priority)
from task_priorities tp2
where tp2.task_id = t.task_id
)
This doesn't need much more optimization than an index on task_priorities(task_id, priority).
I have these Tables :
Livestream
----------
id_livestream int primary key,
name_match varchar(255),
date_match varchar(255),
time_match varchar(255),
league_type int,
tour varchar(255),
stadium int,
id_team1 int,
id_team2 int,
live_video varchar(255),
Team
------
id_team int primary key,
name_team varchar(255),
image_team varchar(255)
League
------
id_league int primary key,
name_league varchar(255)
staduim
-------
id_stadium int primary key,
name_stadium varchar(255)
I am using this sql query to get the data from the tables as below :
select id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video
from Livestream,League,staduim,IsLive
where Livestream.league_type=League.id_league and Livestream.stadium=staduim.id_stadium
What i am getting :
id_livestream|name_match|date_match|time_match|name_league|tour|stadium
65 BarcaMatch 9/5/2017 20:45 League1 22 CampNou
This query is going well but i don t know how to alter this query to :
select the name team,image_team from Team for both id_team1,id_team2 from Livestream
UPDATED : i have searched and found this query to get what i want but i can t add it to my first query :
SELECT
lm.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
lm.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2
FROM Livestream lm
INNER JOIN Team t1
ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
ON lm.id_team2 = t2.id_team
UPDATE 2 :
Please i need help
Simply rewrite the whole query in explizite join Format:
SELECT
lm.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
lm.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2,
id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video
FROM Livestream lm
INNER JOIN Team t1
ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
ON lm.id_team2 = t2.id_team
JOIN League on lm.league_type=League.league
JOIN staduim on lm.stadium=staduim.id_stadium
I want to get data from two tables in my DB. Here's what my tables might look like:
subject_code: semester, subjectcode, subjectname
markmanagment: subjectcode, semester, marks, rollno
i want to select rollno value=1 and semester value=1 and its corresponding values of marks and subject code from table markmanagment and for the same value of semester and subject code i want the subjectname from subject_code
This is a very basic join. Use an INNER JOIN to see only the results that have records in both tables - I used the subject code in the ON statement but you could also use semester here. Then specify your other conditions in the WHERE clause. In your SELECT statement, specify the columns you want to see returned by listing them in the following format: tablename.columnname. I use * here to return all columns.
SELECT *
FROM subject_code sc
INNER JOIN markmanagement mm ON sc.subjectcode = mm.subjectcode
WHERE sc.semester = mm.semester
AND sc.semester = 1
AND mm.rollno = 1
You should do a join table (Inner - Right - Left or Full, depending on your table).
Like :
SELECT * FROM subject_code
INNER JOIN markmanagment
ON subject_code.subjectcode = markmanagment.subjectcode;
WHERE markmanagment.rollno = 1 AND subject_code.semester = 1
You can specify multiple conditions in the on part of a join. For example:
select *
from markmanagement mm
join subject_code sc
on mm.subjectcode = sc.subjectcode
and mm.semester = sc.semester
where mm.semester = 1
and mm.rollno = 1
Hoping, i understood your problem correctly.
Tried to create same problem.
Sqlfiddle link - http://sqlfiddle.com/#!9/5a074b/2/0
create table subject_code
(
semester int,
subjectcode varchar(100),
subjectname varchar(100)
);
insert into subject_code values(1,'S01','subject1');
insert into subject_code values(1,'S02','subject2');
insert into subject_code values(2,'S01','subject1');
create table markmanagment
(
subjectcode varchar(100),
semester int,
marks int ,
rollno int
);
insert into markmanagment values('S01',1,75,1);
insert into markmanagment values('S02',1,80,1);
insert into markmanagment values('S01',2,85,1);
I think below query will help
select b.marks , b.subjectcode , a.subjectname
from subject_code a, markmanagment b
where a.semester = b.semester
and a.subjectcode = b.subjectcode
and a.semester = 1
and b.rollno = 1;
list employees names (Ename) who have both 49008 zip code customers and 49009 zip code customers.
I am struggling to answer the above query based on the above tables.
If the names match between tables assume constraint.
I can filter down to name and zip easily by left joins and group by but struggle after that, I don't know the proper where or having statement. I am assuming it could be done better by a sub query but not sure. Ideally, a single query.
Please and thank you.
1) Create and insert statements for example data:
Create table Employees (EM_Eno INT NOT NULL, EM_Ename VARCHAR(50), EM_Hire_Date DATE, PRIMARY KEY(EM_Eno));
Create table Customers (Customers_Cno INT NOT NULL, Customers_Cname VARCHAR(50), Customers_Street VARCHAR(50), Customers_Zip INT, Customers_Phone INT, primary key(Customers_Cno));
Create table Orders (Orders_Ono INT NOT NULL, Orders_Cno INT, Orders_Eno INT, Orders_Received DATE, Orders_Shipped DATE, primary key(Orders_Ono));
insert into Orders values
( 1,301,501,20161010,20161011);
( 2,302,501,20161011,20161012);
( 3,303,502,20161110,20161111);
( 4,304,502,20161110,20161112);
( 5,305,502,20161110,20161113);
( 6,306,503,20161112,20161114);
( 7,307,501,20161112,20161113);
( 8,308,503,20161112,20161115);
( 9,309,503,20161115,20161120);
(10,300,501,20161112,20161113);
insert into Customers values
(300,'Bryan','100 street',49009,1234567890),
(301,'Ryan','101 street',49008,1234567890),
(302,'Nick','102 street',49009,1234567890),
(303,'Nicholas','103 street',49009,1234567890),
(304,'Alexa','104 street',49009,1234567890),
(305,'Tori','105 street',49008,1234567890),
(306,'Scarlet','106 street',49008,1234567890),
(307,'Heather','100 street',49009,1234567890),
(308,'Amanda','107 street',49008,1234567890),
(309,'James','108 street',49008,1234567890);
insert into Employees values
(501,'Robert',20041010),
(502,'Sam',20050110),
(503,'Brandy',20050710);
2) Ideal end result is answering the query "list employees (names) who have both 49008-zipcode customers and 49009-zipcode customers."
3) Best Attempt thus far:
select Employees.EM_Ename
, Customers.Customers_Zip
from Employees
left
join Orders
on Employees.EM_Eno = Orders.Orders_Eno
left
join Customers
on Orders.Orders_Cno = Customers.Customers_Cno
group
by Employees.EM_Ename
, Customers.Customers_Zip;
The table names are altered slightly in the rextest, because other tables already exist there with the same name...
SELECT e.*
FROM tbl_employees e
JOIN tbl_orders o
ON o.orders_eno = e.em_eno
JOIN tbl_customers c
ON c.Customers_Cno = o.Orders_Cno
WHERE c.Customers_Zip IN(49008,49009)
GROUP
BY e.em_eno
HAVING COUNT(DISTINCT customers_zip) = 2;
http://rextester.com/HCNLU51847
This should be what you want:
select Min(e.Ename)
from #Employees e
join #Orders o on o.Eno = e.Eno
join #Customers c on c.Cno = o.Cno
join #Orders o2 on o2.Eno = e.Eno
join #Customers c2 on c2.Cno = o2.Cno
where o.Ono !=o2.Ono--c.Cno != c2.Cno and
and c.Zip = 49008 and c2.Zip = 49009
group by e.Ename,c.Zip
order by e.Ename
As you can see if you wanted more than those two the self joins become longer.