I have these two simple tables in MS access or MySQL.
I want to find what are the products ordered by customer B ?
what SQL query should i write?
I tried this.. why its wrong!!
SELECT PRODUCT FROM table1
INNER JOIN table2
ON table1.CUST_ID=table2.CUST_ID
WHERE table1.NAME='B' ;
This may be a very easy question, but please answer...i want to learn, Thank you very much..
You can use exists
select
product
from product p
where exists (
select
cust_id
from customer c
where p.cust_id = c.cust_id
and name = 'B'
)
output:
| product |
| ------- |
| K |
| M |
Please use below query,
select * from table1 t1
inner join table2 t2
on (t1.cust_id = t2.cust_id)
where
t1.name = 'B'
Related
Trying to recreate the problem below (not including IN clause and sub-queries):
/* this is query 1 */
Select A.column_1,B.keyValue from table1 as A, table2 as B where
A.someColumn = B.someColumn and B.someotherColumn = 10
/* query 1 gives */
column1 | keyValue
_________________
data-A1 | key-1
data-A2 | key-2
data-A3 | key-3
/* this is query 2 */
Select AVG(ratings) as ratings, C.keyValue from table3 as C,
table4 as D where C.someColumn = D.someColumn and D.someotherColumn = 'abc'
/* query 2 gives */
ratings | keyValue
_________________
rating-1 | key-1
rating-2 | key-2
rating-3 | key-3
/* this is the desired result */
column1 | ratings | keyValue
_________________
data-A1 | rating-1 | key-1
data-A2 | rating-2 | key-2
data-A3 | rating-3 | key-3
I googled it up, found mysql join is the solution
SELECT table1.id, table2.column1, table1.column2 FROM table1
INNER JOIN table2 ON table1.id = table2.id;
But this a very basic example involving only two tables, my first query actually involves 5 tables and second query involves 4 tables with multiple WHERE and IN clauses + sub queries. I am unable to implement this JOIN logic with my complex queries having. This is what I tried but its giving me an error after "JOIN" keyword :
Select * from (Select A.column_1,B.keyValue from table1 as A, table2 as B
where A.someColumn = B.someColumn and B.someotherColumn = 10) as first
JOIN
Select * from (Select AVG(ratings) as ratings, C.keyValue from table3 as C,
table4 as D where C.someColumn = D.someColumn and D.someotherColumn = 'abc')
as second ON first.keyValue = second.keyValue;
Any help would be appreciated.
I don't know the structure of your 4 tables but based on your 2 queries, you can do the following:
select
X.column_1,
Y.ratings,
X.key_value
From
(
Select
A.column_1,
B.keyValue
from
table1 as A,
table2 as B
where
A.someColumn = B.someColumn
and B.someotherColumn = 10
)
X
INNER JOIN
(
Select
AVG(ratings) as ratings,
C.keyValue
from
table3 as C,
table4 as D
where
C.someColumn = D.someColumn
and D.someotherColumn = 'abc'
)
Y
on X.keyvalue = Y.keyvalue;
X and Y are called derived tables.
P.S: It might be possible to create a better query if we know the structure of your underlying tables, sample data and what you are trying to achieve. This is the best answer I can give you based on the information.
I have this table:
ID | Part
1 | A
1 | B
1 | C
2 | B
2 | C
3 | A
3 | D
3 | E
4 | B
4 | D
and want a query that will grab all ID's that have an A, and return a list of all other parts with that ID.
e.g: Want Parts related to B:
Part | Count
A | 1
C | 2
D | 1
What I have currently:
SELECT * FROM tble WHERE ID IN (SELECT DISTINCT ID FROM tble t WHERE Part = ?)
GROUP BY Part ORDER BY COUNT(Part) DESC
This works, but is quite slow and I'm looking to improve it, but having difficulty
Your query is not unreasonable, although the distinct is unnecessary and I would use exists rather than in. And, the outer select needs to be fixed for the aggregation
SELECT t.part, COUNT(*)
FROM tble t
WHERE EXISTS (SELECT 1 FROM tble t2 WHERE t2.ID = t.ID AND t2.Part = ?)
GROUP BY t.Part
ORDER BY COUNT(*) DESC;
Then, to optimize this query, you want an index:
create index idx_tble_id_part on tble(id, part);
Simplify this.. Once you have the logic down, then add back in the SELECT * FROM..
SELECT Part, COUNT(Part) As Count_of_Part
GROUP BY Part ORDER BY COUNT(Part) DESC
Do a join from the table back to itself on ID, and then count the distinct values that pop up:
SELECT b.part, COUNT(DISTINCT b.id)
FROM
table as a
INNER JOIN table as b ON
a.id = b.id AND
a.part <> b.part
WHERE
a.part = 'B'
GROUP BY b.part
This can be simply done by joining back to the table:
SELECT t1.part
,count(*)
FROM tble t1
INNER JOIN tble t ON t.id = t1.id
AND t.part = 'B'
AND t1.part <> t.part
GROUP BY t1.part
SQL Fiddle Demo
You should be able to do this by grouping the data.
Try something like this:
SELECT part, COUNT(id) AS TotalCountId
FROM TABLE_NAME
GROUP BY ID
I have 2 table.
first table
| idgroup | namegroup
second table
| idrequest | col1 | col2 | N1 | N2 | date_extract |
I want to join the tables with concat_ws
SELECT
tb1.*,tb2.*,
CONCAT_WS("_",tb2.N1, tb2.N2) AS GR,
FROM
table2 tb2
INNER JOIN table1 tb1 ON tb1.namegroup= tb2.GR
WHERE
tb2.date_extract = "2015-02-13"
Is it possible? then how?
I am giving a blind try. Nothing changed much except a date() conversion to tb2.date_extract-taking the date value if date_extract is of type TIMESTAMP or DATETIME.
SELECT
tb1.*,tb2.*,
CONCAT_WS("_",tb2.`N1`, tb2.`N2`) AS `GR`,
FROM
table2 tb2
INNER JOIN table1 tb1 ON tb1.`namegroup`= tb2.GR
WHERE
date(tb2.`date_extract`) = "2015-02-13"
If this is not your problem, then please post the error message with the question.
UPDATE: You can simply use GR instead of tbl2.GR need to use the namespace for the result
SELECT
tb1.*, tb2.*, CONCAT_WS("_",tb2.`N1`, tb2.`N2`) AS `GR`,
FROM table2 tb2
INNER JOIN table1 tb1 ON tb1.`namegroup`= `GR`
WHERE
date(tb2.`date_extract`) = "2015-02-13"
-now let me know how it goes?
I'm trying to come up with a MySQL query that will insert data in a table with data from a other table.
That's my structure:
table1:
ID killerid playerid weapon
table2:
ID playername
Now I want to insert into table1 in collumn killerid and playerid the IDs of the two names in a where clause.
But it won't work..
INSERT INTO table1 (killerid)
SELECT
t1.id
FROM
table1 t1
LEFT JOIN
table2 t2 ON (t2.killerid = t1.id)
WHERE t2.playername = 'example'
Better to understand:
I hope thats a little bit better to understand:
There is my table USERS
ID | playername [...]
-----|-------------------
1 | example1
2 | example2
And there is my table KILLS
ID | killerid | victimid | weapon
-----|----------------------------------
1 | 1 | 2 | FIST
2 | 2 | 1 | PISTOL
I want to insert into KILLERS the IDs of the killer (killerid) and the victim (victimid) but I only have the 2 names (example1 and example2) when I'm inserting.
So I have to get the IDs of that 2 names in my table USERS.
Like (pseudo example):
INSERT INTO `KILLERS`(`killerid`, `victimid`, `weapon`)
VALUES(
USERS.ID WHERE USERS.playername = 'example1',
USERS.ID WHERE USERS.playername = 'example2',
'FIST'
);
That should be insert following:
ID | killerid | victimid | weapon
-----|----------------------------------
1 | 1 | 2 | FIST
I hope that's clear.
Given the data you've got it looks like you can get part way there as follows:
INSERT INTO table2(killerid, playerid, weapon)
SELECT t1.id as killerid, -- the 'as killerid' is not required but is useful
t1.playerid, -- to help understand what's intended
t1.weapon
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t2.killerid = t1.id
WHERE t2.playername = 'example'
EDIT
Now that we've got a table structure the solution is much easier to figure out.
INSERT INTO kills (killerid, victimid, weapon)
SELECT u1.ID as killerid,
u2.ID as victimid,
d.weapon
FROM USERS u1
INNER JOIN USERS u2
ON 1 = 1
INNER JOIN (SELECT 'FIST' as weapon
FROM DUAL) d
ON 1 = 1
WHERE u1.playername = 'example1' AND
u2.playername = 'example2';
SQLFiddle here
Share and enjoy.
your sql says:
you need have in varo_lastkills table some values like varo_users table, if you don't have congruent values your query don't run
umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]