473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

having mySQL find only orders with multiple rows in another table

This is a tough question to ask.
I need to pull up a report of all the orders in our e-commerce site,
that has more than one item attributed to it.
orders.ordernum, orderitems.itemnumber orderitems.ordernum is shared
with the same field in the orders table.

In PHP I could just do a querey of all ordernumbers and within the
WHILE do a querey of orderitems where the ordernum equals the original
querey, and then do what I need IF numrows > 1.

But surely there's a way to maybe do that in the mySQL querey? Some way
that might be more efficient than a querey of EVERY order?

If there's simply a name of some topic in mySQL that might apply, just
tell me to look that up. For example: "Take a look for 'distinct',
newbie." =)

Thanks for any help!
Liam

Jul 23 '05 #1
2 1855
On Wed, 13 Jul 2005 10:34:07 -0700, news wrote:
This is a tough question to ask.
I need to pull up a report of all the orders in our e-commerce site,
that has more than one item attributed to it. orders.ordernum,
orderitems.itemnumber orderitems.ordernum is shared with the same field
in the orders table.

But surely there's a way to maybe do that in the mySQL querey? Some way
that might be more efficient than a querey of EVERY order?

Don't know why that should be tough to ask. If I understand it correctly,
it's easy enough to answer. To get a simple list of order numbers that are
associated with more than 1 single item, I would suggest -

SELECT ordernum
FROM orderitems
WHERE COUNT(*) > 1
GROUP BY ordernum;
In PHP I could just do a querey of all ordernumbers and within the WHILE
do a querey of orderitems where the ordernum equals the original querey,
and then do what I need IF numrows > 1.
I'm sure that works fine. But I would hazard a beer bet that says the
MySQL engine, when queried as shown, will deliver the answer substantially
faster than your PHP program loop.

And

If MySQL is running on a separate server - using the PHP code you mention
requires that the server push all records down the network pipes just so
your PHP code can examine the data further and subselect the order numbers
you seek. Use of the SELECT query shown asks the server to deliver only
the data you want. IOW - putting the task to MySQL saves bandwith and is
more network friendly.
If there's simply a name of some topic in mySQL that might apply, just
tell me to look that up. For example: "Take a look for 'distinct',
newbie." =)


You would probably like to become more familiar with SQL "Aggregate
Functions". Of these, the function COUNT() is probably the most used and
useful.

Although you will doubtless discover more that will make you happy :-)
Thomas Bartkus

Jul 23 '05 #2
Thomas Bartkus wrote:
On Wed, 13 Jul 2005 10:34:07 -0700, news wrote:
This is a tough question to ask.
I need to pull up a report of all the orders in our e-commerce site,
that has more than one item attributed to it. orders.ordernum,
orderitems.itemnumber orderitems.ordernum is shared with the same field
in the orders table.

But surely there's a way to maybe do that in the mySQL querey? Some way
that might be more efficient than a querey of EVERY order?

Don't know why that should be tough to ask. If I understand it correctly,
it's easy enough to answer. To get a simple list of order numbers that are
associated with more than 1 single item, I would suggest -

SELECT ordernum
FROM orderitems
WHERE COUNT(*) > 1
GROUP BY ordernum;

--snip-- You would probably like to become more familiar with SQL "Aggregate
Functions". Of these, the function COUNT() is probably the most used and
useful.

Although you will doubtless discover more that will make you happy :-)


Thanks for the help!
That querey as written kept giving me a "invalid use of group function"
error, but I did research into COUNT and GROUP BY thanks to you, and
came up with:

SELECT ordernum, COUNT(ordernum)
FROM orderscart
GROUP BY ordernum
HAVING COUNT(ordernum)>1;

and that worked perfectly. I'm seeing a lot of cool and powerful things
I can do with queries like this. =)

Thanks again!!
Liam

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gary | last post by:
I haver a table of students - Say 100 students that I need to be able to update/delete and amend. I know I can do this one student at a time which is simple but lets say I want to see all the...
6
by: jacob nikom | last post by:
I would like to create data model for a group of stores. All stores in this group are very similar to each other, so it is natural to allocate one MySQL database per store. Each database is going...
2
by: Christian Höhener | last post by:
Hi I have a given attributvalue of a specific attribut (sys_obid=81653) which occurs in multiple tables. Now, I should know the tablename of the table in which this attribut occurs. Which...
0
by: Chris | last post by:
Hi, I am currently designing a simple service orders database. I have played around with MySQL a bit but this is the first time I'm using it in anger, I have a few design queries to make sure I am...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
0
by: taras.di | last post by:
I'm trying to understand why MySQL isn't using all parts of the primary key when I add in an OR statement in the query. I have a people database, and a distances database. The people database...
1
by: michael.martinek | last post by:
Greetings! I've recently been trying to do something, which apparently looks like it may be a little odd.. I'm not finding anything in the manuals or anywhere on the web where something similiar...
4
by: Federico | last post by:
Hi everybody, I'm evaluating the possibility of using MySQL 5.0 as a database backend for an application we are doing and I'd like to have hardware requirements rough estimates and/or real world...
12
by: dino d. | last post by:
hi everyone- my subject pretty much says it all- is there a secure way to do this? the non-secure ways are, as i understand it, to populate a listbox with indices as names, or maybe use a hidden...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.