[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL Query - Creating a subquery from two tables

Posted on 2014-08-24
10
Medium Priority
?
309 Views
Last Modified: 2014-08-24
I have two tables:

autoCoupons
CouProducts

Both have the column brand.

I want all the duplicate brands from AutoCoupons with the source shoprite.com from CouProducts.

Here is what I was trying

select * from `couProducts` where `brand` in (select 'brand' from `autoCoupons` where `service` = "Shoprite.com");

This is not working.  Can anyone help?
0
Comment
Question by:chrispre
  • 5
  • 4
10 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 40282094
You need to use apostrophes.
select * from `couProducts` where `brand` in (select 'brand' from `autoCoupons` where `service` = 'Shoprite.com');

Open in new window

The very same query can be written using INNER JOIN
select c.* from `couProducts` c INNER JOIN `autoCoupons` a ONwhere a.`brand` = c.'brand' 
where a.`service` = 'Shoprite.com';

Open in new window

However, the question is not clear. What do you mean by:
I want all the duplicate brands
Can you elaborate? Can you show the sample data and the desired result?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40282109
This is not working.  Can anyone help?
How do you know it is not working?  How are you determining what error is occurring?
0
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 40282119
Found the error. In the sub-query you used apostrophes around 'brand' instead of a back-quote symbol `:
select * from `couProducts` where `brand` in (select 'brand' from `autoCoupons` where `service` = 'Shoprite.com');
It needs to be:
select * from `couProducts` where `brand` in (select `brand` from `autoCoupons` where `service` = 'Shoprite.com');

Open in new window

Otherwise, the subquery will return the word "brand" for every row:
brand
brand
brand
brand

Open in new window

0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:chrispre
ID: 40282144
I tried this and it did run but got

MySQL returned an empty result set (i.e. zero rows). ( Query took 0.3850 sec )

I know there are duplicates in both tables..... So there must be something wrong with what I am requesting.....
0
 

Author Comment

by:chrispre
ID: 40282146
So both the query I posted and the query from Chaau do work, but I must be missing something else, like I said I know there are duplicates.
0
 
LVL 25

Expert Comment

by:chaau
ID: 40282150
Have you tried my last query.
BTW, in my original INNER JOIN query I have used your 'brand' instead of `brand` (cut-n-paste typo). The correct INNER JOIN query should be:
select c.* from `couProducts` c INNER JOIN `autoCoupons` a ON a.`brand` = c.`brand` 
where a.`service` = 'Shoprite.com';

Open in new window

0
 

Author Comment

by:chrispre
ID: 40282168
Ha ha ha ha ok found the problem.  The scraped data going into the database had spaces before and after brand name.  Thank you both for replying
0
 

Author Comment

by:chrispre
ID: 40282182
Well it is only now returning 2 results where there should be 4, oye
0
 
LVL 25

Expert Comment

by:chaau
ID: 40282195
Is it possible that the "Shoprite.com" is recorded using different cases? Like "Shoprite.com" and "shoprite.com"? If it is then you need to consider this:
select * from `couProducts` 
where `brand` in 
   (select `brand` from `autoCoupons` where LOWER(`service`) = 'shoprite.com');

Open in new window

Similarly, if the brands do not match 100%, use this:
select * from `couProducts` 
where LOWER(TRIM(`brand`)) in 
   (select LOWER(TRIM(`brand`)) from `autoCoupons` where LOWER(`service`) = 'shoprite.com');

Open in new window

0
 
LVL 25

Expert Comment

by:chaau
ID: 40282197
As I said before, sample data and the desired result would help to fix the problem in one go
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question