We have a Web Analytics database with 2 tables Table 1 and Table 2 as seen below.
We really could do with some inputs here to display the desired result as a single row.
Many Thanks
X
SELECT
A.Name,
A.ref_num Policy_number,
B.QNum Quote_number
FROM
(
SELECT AA.Name,BB.ref_num FROM
(SELECT Name,ref_num FROM Table1 WHERE Name='Purchase' AND Cookieid=123456) AA
INNER JOIN Table2 BB USING (Reference)
) A,
(
SELECT BB.ref_num QNum FROM
(SELECT * FROM Table1 WHERE Name='Quote' AND Cookieid=123456) AA
INNER JOIN Table2 BB USING (Reference)
) B
;
All you have to do is set the Cookieid in both subqueries.
Make sure you have this index:
ALTER TABLE Table1 ADD INDEX Cookieid_Name_ndx (Cookieid,Name);
Related
I have two mysql tabales.
Table1:opened_datatable
Table2:unidata
Table1 has only one column:Emails
Table2 has 45 columns, three of them are:Email_Office, Email_Personal1, Email_Personal2
I want to fetch full rows from Table2-unidata if Emails column of Table1 matches with either Email_Office or Email_Personal1 or Email_Personal2. I am getting little bit confused here.I tried this way:
select a.emails
from opened_datatable a
where a.Emails in (select *
from unidata b
where b.email_office=a.emails
or b.Email_Personal1=a.emails
or b.Email_Personal2=a.Emails
)
Its showing only one row of first table while I want to show matched rows of Table2 -unidata. First I need to mention table 2 and then I should have to match it with table 1-opened_datatable. But how can I do that?
Try This:
SELECT a.emails, b.*
FROM opened_datatable a
INNER JOIN unidata b ON a.emails IN (b.email_office, b.Email_Personal1, b.Email_Personal2)
Your current query should return an error.
Try a Corrrelated Subquery using EXISTS, quite similar to your apporach:
select a.emails
from opened_datatable a
where EXISTS
( select *
from unidata b
where b.email_office=a.emails
or b.Email_Personal1=a.emails
or b.Email_Personal2=a.Emails
)
You will probably not get good performance due to the OR-ed conditions.
Edit:
If performance is too bad, you might try a UNION approach:
select a.emails
from opened_datatable a
where a.emails
IN
( select email_office
from unidata b
UNION
select Email_Personal1
from unidata b
UNION
select b.Email_Personal2
from unidata b
)
I have two tables, who have the exact same columns. I want to merge table b into table a and if the dataset has the same ID, I want to use the dataset of table b.
I tried something like:
SELECT *
FROM
((SELECT
*
FROM
tableA) UNION (SELECT
*
FROM
tableB)) AS temp
GROUP BY temp.ID
ORDER BY temp.ID
but that gave me a mix of both tables.
You can do this using union all along with some additional logic:
select b.*
from b
union all
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
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"
the case is that I need to select a field distinct from table1 (no duplicates) and use the result as a key to select from another table2. And I need this to be in one query. Is this possible?!
table1: hID, hName, hLocation
table2: hID, hFrom, hTo, hRate, hRoomType, hMeals
I want to correct version of this query:
SELECT
*
FROM
table1
JOIN (
DISTINCT
hID
FROM
table2
WHERE
hRoomType = Double Room
ON table1.hID = table2.hID)
expected result: all hotels that offer Double Room thanks much –
thanks for help!
Your question is quite vague and confusing. Is this what you are looking for:
SELECT hID, name, location
FROM table2
INNER JOIN table1
ON table1.hID = table2.hID
GROUP BY table2.hID;
Here is a skeleton to achieve this:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM (
-- This is the distinct list from table1
SELECT DISTINCT
id
FROM
table1 T1
) DT1
INNER JOIN table2 T2
ON T1.id = T2.reference_to_t1_id
Another solution if you don't want to retrieve any columns from table1:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM
table2 T2
WHERE
-- Sais that get all record from table2 where this condition matches
-- at least one record
EXISTS (
SELECT 1 FROM table1 T1 WHERE T1.id = T2.reference_to_t1_id
)
For your tables and question
SELECT
hID, hName, hLocation
FROM
table1 T1
WHERE
EXISTS (
SELECT 1 FROM
table2 T2
WHERE
T1.hID = T2.hID
AND T.hRoomType = 'Double' -- Assuming that this is the definition of double rooms
)
I have two table :- table a, table b.
table a
---ID---
1
2
3
4
5
7
table b
---ID----
2
3
4
5
6
I have to get Output Like this without UNION Command:-
----ID-----
1
2
3
4
5
6
7
Note: I have one solution with union:-
**select * from a
UNION
select * from b;**
I need alternative to this. please experts suggest.
We need another table with (at least) 2 rows for this:
CREATE TABLE d
( id INT NOT NULL
);
INSERT INTO d
(id)
VALUES
(0), (1) ;
Then, if we want to have only one query, we can use (this is for fun, DO NOT USE in production, that's why we have UNION):
SELECT DISTINCT
COALESCE(aa.id, bb.id) AS id
FROM
d
LEFT JOIN a AS aa ON d.id = 0
LEFT JOIN b AS bb ON d.id = 1
WHERE
COALESCE(aa.id, bb.id) IS NOT NULL
ORDER BY
id ;
Tested at SQLfiddle.com, and for other table combinations:
1 row - 1 row
2 rows - 2 rows
0 rows - 1 row
0 rows - 2 rows
0 rows - 0 rows
try this:
I think it works well in MS-SQL, change it to MySQL if you need, but MYSql doesnot support full outer join! Good luck
SELECT (
CASE
WHEN b.ID IS NULL
THEN a.ID
WHEN b.ID=a.ID
THEN b.ID
ELSE b.ID
END)
FROM
(SELECT ID FROM table2
)b
FULL OUTER JOIN
(SELECT ID FROM table1
) a
ON a.ID=b.ID
and play around with the query
Fiddle: http://sqlfiddle.com/#!3/c657d/13
And here is the MYSQL version:
SELECT DISTINCT COALESCE(t1.id, t2.id) id
FROM
(
SELECT TABLE_NAME <> 'table_a' n
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = SCHEMA()
AND TABLE_NAME IN('table_a', 'table_b')
) t LEFT JOIN table_a t1
ON t.n = 0 LEFT JOIN table_b t2
ON t.n = 1
Working fiddle: http://sqlfiddle.com/#!2/c657d8/34
I don't know why you are avoiding UNION but you can do like following
CREATE TABLE temp_ids(ID INT);
INSERT INTO temp_ids SELECT ID FROM a;
INSERT INTO temp_ids SELECT ID FROM b;
SELECT DISTINCT ID FROM temp_ids;
Try a full outer join and filter the NULL values.
As an abstract exercise (if this is an interview question we expect a kickback!) one ugly, innefficient solution would be to create a cartesian product and filter the unique values:
SELECT DISTINCT IF(a<>b, b.id, a.id)
FROM a, b
ORDER BY 1
;
Use FULL OUTER JOIN, like this:
SELECT CASE
WHEN t1.id IS NULL THEN t2.id
ELSE t1.id
END AS id
FROM t1
FULL OUTER JOIN t2
ON t1.id = t2.id
ORDER BY id
Note: Mysql does not support full outer joins.
Working demo: http://sqlfiddle.com/#!6/b7684/10