I have two tables;
database tftube;
table tftube_video;
database tfmember;
table member;
create table member(
no int primary key AUTO_INCREMENT,
member_no int,
name varchar(15) not null
);
create table tftube_reply(
no int primary key AUTO_INCREMENT,
member_no int,
video_name varchar(100)
);
I need to
1.tfmember.member.member_no equals tftube.tftube_reply.member_no
2.video_name=10.
3.Result variables are name, no, member_no,video_name.
So I try to this way.
select a.name from tfmember.member a join tftube.tftube_reply b on a.no = b.member_no where b.video_name=10;
but it's error
please help me
p.s tfmember.member.member_no contains tftube.tftube_reply.member_no.
Correct query will be:
select a.name from member a join tftube_reply b on a.no = b.member_no and b.video_name='10';
You should learn more about query before posting to ST.
Refer below link:
https://www.tutorialspoint.com/sql/index.htm
select a.name from tfmember.member a join tftube.tftube_reply b on a.no = b.member_no where b.video_name=10;
You join with table a auto increment col with table b member_no and video_name is varchar type that's why use '10' in where condition
Correct Query
select a.name from tfmember.member a join tftube.tftube_reply b on a.member_no = b.member_no where b.video_name='10';
Related
I have a database with the following base structure.
create table objects
(
id int auto_increment primary key,
);
create table object_attribute_values
(
id int auto_increment primary key,
object_id int not null,
attribute_id int not null,
value varchar(255) null
);
create table attributes
(
id int auto_increment primary key,
attribute varchar(20) null,
);
And so let's say the attribute table has 3 :
id
attribute
1
color
2
rating
3
size
I need select all objects that have color='black', rating IN (5, 10), size=10.
I understand how to get all objects in black
SELECT o.id
FROM objects o
INNER JOIN object_attribute_values oav ON oav.object_id = o.id
INNER JOIN join attributes a ON a.id = oav.attribute_id
WHERE a.attribute = 'color' AND oav.value = 'black'
The result should be like this:
object_id
attributes
1
color:black,rating:6,size:10
7
color:black,rating:6,size:10
12
color:black,rating:9,size:10
What you are dealing with is a key/value table. I don't like them much, because they make querying data more complex and don't guarantee consisteny (data type, obligatory/optional values) as normal columns do. But sometimes they are necessary.
Anyway, the typical way to query key/value tables is by aggregation:
SELECT
o.id as object_id,
GROUP_CONCAT(CONCAT(a.attribute, ':', oav.value) ORDER BY a.id SEPARATOR ';') AS attributes
FROM objects o
INNER JOIN object_attribute_values oav ON oav.object_id = o.id
INNER JOIN join attributes a ON a.id = oav.attribute_id
GROUP BY o.id
HAVING SUM(a.attribute = 'color' AND oav.value = 'black') > 0;
The HAVING clause looks for all objetcs that have color = black. Others are dismissed. This works, because in MySQL true = 1, false = 0, so we can just add up the condition results.
Yes, you can do this. The knack you need is the concept that there are two ways of getting tables out of the table server. One way is ..
FROM TABLE A
The other way is
FROM (SELECT col as name1, col2 as name2 FROM ...) B
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
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.
How to get the Unmatched data from two table.I want to display the data which is not matched with CostomerMaster table. here is my table structure is
CustomerMaster Table:
CusID int Unchecked
CName varchar(MAX) Checked
Caddress varchar(50) Checked
Cloacation varchar(50) Checked
CMobile varchar(50) Checked
DailyDispatch Table:
DailyDispatchID int Unchecked
DcNo varchar(50) Checked
CustID varchar(50) Checked
Name varchar(50) Checked
OrderDate varchar(50) Checked
Gas varchar(50) Checked
I tried this:
SELECT * FROM customermaster C WHERE NOT EXISTS (SELECT 1 FROM dailydispatch D WHERE C.CNAME = D.NAME)
i am getting the output,but i need to display the output like
Name Gas OrderDate
The problem is how to display in the format,i used joins but not worked out any ideas for sort it out.
Try this.
SELECT DD.Name, DD.Gas, DD.OrderDate
FROM dailydispatch DD full outer join customermaster C on C.CNAME = DD.NAME)
Try this...
SELECT D.Name, D.Gas, D.OrderDate FROM DailyDispatch D
Left Join CustomerMaster C On C.CNAME = D.NAME
And C.CustID Is Null
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