Mysql Inner join with concat_ws - mysql

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?

Related

How to join two tables with one-to-many relationship in SQL?

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'

how to make query show the an other column of the primary key?

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]

Joining 2 tables SQL

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"

Multiple INNER JOIN returns null

I'm working on joining 3 tables.
table1.
grp_id | email_id
1 | 3
1 | 58
table2.
sam_msg_id | sam_subject
3 | Funnel
table3.
id | subject
58 | testing check
Desired Output:
id |grp_id|email_id|sam_subject|subject |
184|1 |3 |funnel | |
185|1 |58 | |testing check|
the query I tried:
SELECT table1.*, table2.sam_subject, table3.*
FROM table1
INNER JOIN table2
ON table2.sam_msg_id = table1.email_id
INNER JOIN table3
ON table3.id = table1.email_id
WHERE table1.grp_id = '1'
What I'm trying to do here is to get the list of subject and its id from table2 and table3 where the id is found at table1 under email_id.
When I tried doing it with one inner join only by checking only the data from table2 it was working.
I am not familiar in using inner joins thus I can't really see what I'm doing wrong.
I am using MySQL.
Sounds like you need a LEFT JOIN
SELECT table1.*, table2.sam_subject, table3.*
FROM table1
LEFT JOIN table2
ON table2.sam_msg_id = table1.email_id
LEFT JOIN table3
ON table3.id = table1.email_id
WHERE table1.grp_id = '1'
Based on you edit, the following should give you the results you want:
SELECT t1.grp_id,
t1.email_id,
coalesce(t2.sam_subject, '') sam_subject,
coalesce(t3.subject, '') subject
FROM t1
left JOIN t2
ON t2.sam_msg_id = t1.email_id
left JOIN t3
ON t1.email_id = t3.id
WHERE t1.grp_id = '1'
See SQL Fiddle with Demo
I'm a bit confused about what you are asking. If you can provide an example of your current data and your desired output we can probably nail down a solution.
As it is, I suspect you need to read up on LEFT JOIN, which will return NULL if there is no matching record in your child table. INNER JOIN will not return a record if there is no match.
Take a look at this article for an explanation of the different types of joins:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
UPDATE:
I'm not sure where the first column of your desired results is coming form but the query below should get you the desired results:
SELECT t1.*, t2.sam_subject, t3.subject
FROM Table1 as t1
LEFT JOIN table2 as t2
on t1.email_id = t2.sam_msg_id
LEFT JOIN table3 as t3
on t1.email_id = t3.id

SQL Query: Joining two tables where entity of the first table has no, or multiple entries in other table

I have to database tables, where entities of the first Table may or may not have associated entries in the second table:
Table 1 Table 2
+-----+-----+ +-----+-------+-------+
| ID | ... | | ID | T1_ID | NAME |
+-----+-----+ +-----+-------+-------+
| 1 | ... | | 1 | 1 | p1 |
| 2 | ... | | 2 | 1 | p2 |
| 3 | ... | | 3 | 2 | p1 |
| 4 | ... | +-----+-------+-------+
+-----+-----+
I have the following queries i need to run:
Get all entities of Table_1 with a specific entry of Table_2 - That's easy, a simple Join will do...
Get all entities of Table_1, which don't have a specific entry of Table_2 associated - not so easy, but i also managed to query this with a join.
Get all entities of Table_1, which have a specific entry (A) and don't have another specific entry (B) associated, i.e. get all entities of Table_1 that have an entity of Table_2 with name=p1 and don't have an entity of Table_2 with name=p2 associated.
Is it possible to accomplish the kind of query from (3) in a single sql-statement without a sub-query?
Get all entities of Table_1, which
have a specific entry (A) and don't
have another specific entry (B)
associated, i.e. get all entities of
Table_1 that have an entity of Table_2
with name=p1 and don't have an entity
of Table_2 with name=p2 associated.
I'm having a bit of trouble understanding your criteria, but I think that is what you want:
SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.t1_id
WHERE t2.name = 'p1'
AND NOT EXISTS(SELECT 'x' FROM Table2 t2_2 WHERE t1.ID = t2_2.t1_id AND t2_2.name = 'p2')
That will give you everything from Table1 that has a matching record in Table2 with name = 'p1' and DOESN'T have a matching record in Table2 with name = 'p2'. Is that what you need?
EDIT AGAIN:
I thought of a smarter way to do this that involves a static (non-correlated) subquery. This subquery will only be executed one time, rather than being executed once for every parent row in Table1. I didn't put this code through a query analyzer, but it should be significantly faster than of the queries using EXISTS(...)
SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.t1_id
WHERE t2.name = 'p1'
AND t1.id NOT IN(SELECT t1_id FROM Table2 WHERE name = 'p2')
You can use an EXISTS subquery (effectively the same as doing two joins).
SELECT * FROM Table_1 AS t1
WHERE EXISTS (SELECT * FROM Table_2 AS t2 WHERE t1.Id = t2.Id AND Name='p1')
AND NOT EXISTS (SELECT * FROM Table_2 AS t2 WHERE t1.Id = t2.Id AND Name='p2')
To get all occurrences where t2 matches t1.id but not some other field do
SELECT t1.id, t2.id FROM table2 t2
INNER JOIN table1 t1 ON (t2.t1_id = t1.id AND not(t2.fieldx <=> t1.fieldx))
Note that this will also exclude rows where both fieldx are null.
If you don't want that substitute the <=> with =.
To make the variation of solutions more complete:
SELECT t1.*
FROM Table_1 t1
INNER JOIN Table_2 it2 ON t1.ID = it2.T1_ID AND it2.NAME = 'p1'
LEFT JOIN Table_2 lt2 ON t1.ID = lt2.T1_ID AND lt2.NAME = 'p2'
WHERE lt2.ID IS NULL