The scenario:
two tables
CustomerTable
---------------
CustomerID
OrderID
CustomerName
CustomerEmail
CustomerPhone
OrderTable
---------------
OrderID
ProductID
ProductName
ProductCost
This database was handed to me and I was asked to solve a problem - it looks
like an inner join solution would apply, but I'm not 100% sure.
There are 14 products total (numbers 1 through 14).
I'm looking to get a list of all the customers who have ordered product #1,
UNLESS they've ordered product #14 in which case I don't want to know about
that customer at all.
Any help would be greatly appreciated! I'll watch the newsgroup for the
answer - hopefully your response can help someone else too. However, if you
prefer to email me directly, you can send it to me at bunchah at yahoo dot
com.
Thanks in advance!
(if it'll help, I'll buy the person offering the correct solution a beer -
pending age verification of course) ;)
-Al
Jul 20 '05
11 2912
select DISTINCT ct.cart_id
from OrderTable ct
inner join CartTable ot
on (ct.cart_id = ot.cart_id)
group by ct.cart_id
having count(case ot.product_inde x when 1 then 1 else null end) > 0
and count(case ot.product_inde x when 14 then 1 else null end) = 0
order by ct.cart_id;
Thanks Andy, that worked PERFECTLY!
Ok, now what kind of beer do you drink? (or wine?)
Dave - thanks to you too - it's nice to know I'm not the only one working on
a Friday night. :)
-Al
"Andy Hassall" <an**@andyh.co. uk> wrote in message
news:du******** *************** *********@4ax.c om... On Sat, 22 Nov 2003 02:06:15 +0000, Andy Hassall <an**@andyh.co. uk> wrote:
A variation on Dave's query to account for MySQL's limitations and
quirks[...] cannot reference aggregates in HAVING clauses only their aliases, [...] OK, that's wrong, looks like I was thinking of something else. So going
closer back to Dave's query:
select DISTINCT ct.CustomerID from CustomerTable ct inner join OrderTable ot on (ct.OrderID = ot.OrderID) group by ct.CustomerID having count(case ot.ProductID when 1 then 1 else null end) > 0 and count(case ot.ProductID when 14 then 1 else null end) = 0 order by ct.CustomerID;
-- Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk) Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
"news-east.earthlink. net" <ab************ ******@yahoo.no spam.com> wrote in message news:<nr******* ***********@new sread2.news.atl .earthlink.net> ... The scenario:
two tables
CustomerTable --------------- CustomerID OrderID CustomerName CustomerEmail CustomerPhone
OrderTable --------------- OrderID ProductID ProductName ProductCost
This database was handed to me and I was asked to solve a problem - it looks like an inner join solution would apply, but I'm not 100% sure.
There are 14 products total (numbers 1 through 14). I'm looking to get a list of all the customers who have ordered product #1, UNLESS they've ordered product #14 in which case I don't want to know about that customer at all.
Any help would be greatly appreciated! I'll watch the newsgroup for the answer - hopefully your response can help someone else too. However, if you prefer to email me directly, you can send it to me at bunchah at yahoo dot com.
Thanks in advance!
(if it'll help, I'll buy the person offering the correct solution a beer - pending age verification of course) ;)
-Al
Hi ,
Here is my reply
select customertable.* c1 from customertable inner join ordertable o1
on c1.orderid = o1.orderid where c1.orderid = 1 and o1.orderid <> 14
With Thanks
Raghuraman This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ike |
last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL
4.0.20-standard. Thus, in trying to expedite the query, I am trying to set
indexes in my tables.
My query requires four inner joins, as follows :
SELECT DISTINCT
upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori
gin,associatekey.username,associatekey2.username,upcards.deleted
FROM upcards,status,origins,associates
INNER JOIN status...
|
by: Prem |
last post by:
Hi,
I am having many problems with inner join. my first problem is :
1) I want to know the precedance while evaluating query with multiple
joins.
eg.
select Employees.FirstName, Employees.LastName, TerritoryID,
Employees.EmployeeID,
RegionID, ProductID
from Employees
|
by: mheydman |
last post by:
I apologize if this has been asked before- I searched google but could
not find a concrete answer.
I recently inherited a database whose t-sql code is written in a format
that I find difficult to read (versus the format I have used for
years).
I have tested the queries below using the SQL Profiler, and both have
identical costs. Is there any advantage of one format over the other?
|
by: Nathan |
last post by:
I have an application that uses an Access database to
gather information on students' test scores. In the
database there are three tables which are joined by one-
to-many relationships: Students, Subjects, and Tests. I
am trying to create a query that joins these three tables
and show all the subjects for only one student, and all
the tests taken in each of those subjects. This is the
query I have entered in the Query Builder:
...
|
by: dmonroe |
last post by:
hi group --
Im having a nested inner join problem with an Access SQl
statement/Query design. Im running the query from ASP and not usng the
access interface at all. Here's the tables:
tblEmployees
empId -- EmpName -- EmpRole -- EmpManager
-------....------------.... ---------....---------------
1........ dan yella..........1..........2
| |
by: MP |
last post by:
Hi
trying to begin to learn database using vb6, ado/adox, mdb format, sql
(not using access...just mdb format via ado)
i need to group the values of multiple fields
- get their possible variations(combination of fields),
- then act on each group in some way ...eg ProcessRs (oRs as RecordSet)...
the following query will get me the distinct groups
|
by: exoskeleton |
last post by:
hi dear experts im here once again...i have a problem on showing the result when im using inner join...here's my code..
$sql_trans_pro_tbl="SELECT transaction_tbl.date_subscribe,transaction_tbl.date_expire".
",product_tbl.pro_id,product_tbl.pro_des FROM transaction_tbl INNER JOIN".
" product_tbl ON transaction_tbl.pro_id=product_tbl.pro_id WHERE".
" transaction_tbl.account_name='$forder_name'";...
|
by: Chamnap |
last post by:
Hello, everyone
I have one question about the standard join and inner join, which one
is faster and more reliable? Can you recommend me to use? Please,
explain me...
Thanks
Chamnap
|
by: Anila |
last post by:
Hi Friends,
My problem with Inner join is ...
first i joined two tables and i got the result.
after that iam trying to join one more table its giving syn tax error in JOIN condition.
Here is the Query
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |