Joining 2 tables SQL - mysql

I m trying to fetch data from 2 table aisle and type. The aisle table has following construct:
aisle_id | name | x | y
and the type table has following construct
type_id | name | aisle_id (FK)
aisle_id is the foreign key.
I'm using this sql query which works but, not as expected:
SELECT NAME,X, Y FROM type, aisle where Name ="bread"
What this statement returns me is, it returns bread but also returns all the data from the X and Y, which i do not want, I just want the x and y related to bread. So could anyone help?
Thank you

Try joining the two on aisle_id as below:
SELECT t.Name, a.X, a.Y
FROM aisle a INNER JOIN type t
ON a.aisle_id = t.aisle_id
WHERE a.Name ="bread"

With a simple Google search, and since you know what you need (join), the first result came directly from MySQL documentation:
Some join examples:
SELECT * FROM table1, table2;
SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;
SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id;
SELECT * FROM table1 LEFT JOIN table2 USING (id);
SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id
LEFT JOIN table3 ON table2.id=table3.id;
In your case it would be:
SELECT aisle.NAME, aisle.X, aisle.Y FROM type JOIN aisle ON type.type_id = aisle.type_id WHERE aisle.Name ="bread"

Related

Is This Join Possible?

I have a couple of tables that look like this.
table_a | table_b
-------------------------
prim_key | prim_key
zero_or_one | value1
valueA | value2
valueB | value3
valueZ |
What I'm hoping to do is retrieve all of the values (prim_key, value1, value2, value3) from TABLE B if the primary keys of each table match and the value of zero_or_one in TABLE A is 0.
I'm completely new to joins, and I'm not exactly sure which join I should be using for this, but it seems like a FULL OUTER JOIN is most appropriate.
SELECT table_b.*
FROM table_a
FULL OUTER JOIN table_b
ON table_a.prim_key = table_b.prim_key
Is this even possible?
Am I using the right join for the job?
Is my "select all" syntax correct?
Since you want entries from table_b only when there is a matching primary key found in the table_a; a simple Inner Join would suffice in this case
SELECT table_b.*
FROM table_b
INNER JOIN table_a
ON table_a.prim_key = table_b.prim_key AND
table_a.zero_or_one = 0
This answer is not meant als a real answer this is meant how to simulate FULL OUTER JOIN in MySQL.
FULL OUTER JOIN is not supported in MySQL you can simulate it with a LEFT JOIN, UNION ALL and RIGHT JOIN
SELECT * FROM table_a LEFT JOIN table_b ON table_a.prim_key = table_b.prim_key
UNION ALL
SELECT * FROM table_a RIGHT JOIN table_b ON table_a.prim_key = table_b.prim_key
WHERE table_a.prim_key IS NULL

How to join two tables, with distinct columns on either side?

I have two tables I'm trying to join to produce a unique set of data for a third table, but having trouble doing this properly.
The left table has an id field, as well as a common join field (a).
The right table has the common join field (a), and another distinct field (b)
I'm trying to extract a result-set of id and b, where neither id nor b are duplicated.
I have an SQL fiddle set up: http://www.sqlfiddle.com/#!9/208de/3/0
The ideal results should be:
id | b
---+---
1 | 1
2 | 2
3 | 3
Each id and b value appears only once (it's only coincidence they match here, that can't be assumed always).
Thanks
What about a CTE along with a DISTINCT, Would that work?
WITH
cte1 (ID, B)
AS
(
SELECT DISTINCT Table1.ID
FROM Table1
WHERE Table1.ID IS NOT NULL
GROUP BY Table1.ID
)
SELECT DISTINCT
Table2.b
FROM Table2 AS sp
INNER JOIN cte1 AS ts
ON sp.b <> ts.ID
ORDER BY ts.ID DESC

Inner join on two different fields of 2 different tables

I have one table which has fields X,Y,Z,BAGID.
The BAGID is in the form of (12345-400) where 12345 is the user's id and 400 is the BAG's id.
I have another table which has fields A,B,C,USERID.
The USERID is in the form of 12345 which is same as the first part of BAGID.
So is it possible to join these two tables on the common USERID and get the fields USERID,X,Y,A,B?
Table 1:
X Y Z BAGID(userid+bagid)
1 2 4 12345-400
Table 2 :
A B C USERID
3 5 7 12345
I want the output as:
X Y A B USERID
1 2 3 5 12345
Is it possible to have a join these two tables?
select Table1.X, Table1.Y, Table2.A, Table2.B, Table2.USERID
from Table1
inner join Table2
on Table1.BAGID = Table2.USERID;
I know i cannot user BAGID and USERID as they are different. But is it possible for me to use the userid part of the BAGID of Table1 which is the same as USERID of Table2?
Any help would be appreciated.
You can use the SUBSTRING_INDEX to extract USERID out of BAGID:
select Table1.X, Table1.Y, Table2.A, Table2.B, Table2.USERID
from Table1
inner join Table2 on SUBSTRING_INDEX(Table1.BAGID, '-', 1) = Table2.USERID
This will work provided that there is only one '-' in BAGID.
Demo here
Sure, just join on LEFT(BAGID,5). Depending on the USERID DataType you may need to CAST it as well.
If the USERID portion of BAGIT is variable length you first need to find the length using INSTR(BAGID, '-')
If you're using t sql you can use the SUBSTRING ( expression ,start , length ) function to get only the first 5 characters of the bag id, and then join on that value. Ie
SELECT *
FROM table1
INNER JOIN table2 ON SUBSTRING(TABLE1.bagid, 0, 5) = table2.userid
If not using t sql, whatever you're using should have a similar substring function
You can inner join on substring of table1 column
Select Table1.X, Table1.Y, Table2.A, Table2.B, Table2.USERID
From Table1
Inner join Table2
ON SUBSTRING_INDEX(Table1.BAGID,'-',1) = Table2.USERID;

Mysql Inner join with concat_ws

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?

3 tables and 2 left joins

Query 1:
SELECT sum(total_revenue_usd)
FROM table1 c
WHERE c.irt1_search_campaign_id IN (
SELECT assign_id
FROM table2 ga
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
)
Query 2:
SELECT sum(total_revenue_usd)
FROM table1 c
LEFT JOIN table2 ga
ON c.irt1_search_campaign_id = ga.assign_id
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
Query 1 gives me the correct result where as I need it in the second style without using 'in'. However Query 2 doesn't give the same result.
How can I change the first query without using 'in' ?
The reason being is that the small query is part of a much larger query, there are other conditions that won't work with 'in'
You could try something along the lines of
SELECT sum(total_revenue_usd)
FROM table1 c
JOIN
(
SELECT DISTINCT ga.assign_id
FROM table2 ga
JOIN table3 d
ON d.campaign_id = ga.assign_id
) x
ON c.irt1_search_campaign_id = x.assign_id
The queries do very different things:
The first query sums the total_revenue_usd from table1 where irt1_search_campaign_id exists in table2 as assign_id. (The outer join to table3 is absolutely unnecessary, by the way, because it doesn't change wether a table2.assign_id exists or not.) As you look for existence in table2, you can of course replace IN with EXISTS.
The second query gets you combinations of table1, table2 and table3. So, in case there are two records in table2 for an entry in table1 and three records in table3 for each of the two table2 records, you will get six records for the one table1 record. Thus you sum its total_revenue_usd sixfold. This is not what you want. Don't join table1 with the other tables.
EDIT: Here is the query using an exists clause. As mentioned, outer joining table3 doesn't alter the results.
Select sum(total_revenue_usd)
from table1 c
where exists
(
select *
from table2 ga
-- left join table3 d on d.campaign_id = ga.assign_id
where ga.assign_id = c.irt1_search_campaign_id
);