473,396 Members | 1,886 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.

MYSQL: GROUP BY Statement

56
Hi all,

We have a product table in the database. One of the columns is ProductGroup which we use in the case when a product is available in multiple colours say a red, green or blue cap. So if a product has multiple colours we add each product to the table but also specify its group.

If the item doesn't have different colours we dont specify a group. Unfortunately we cannot modify the database the default value of the productGroup column is an empty string (actually it is a Foxpro database but my question is more of a SQL statement problem)

I need to list all products those with a ProductGroup and those without a ProductGroup. Now my problem is I need those which have a ProductGroup to only show the first record. GROUP BY does this but does not return the other rows as unique groups, since it automatically groups the products with no ProductGroup.

Can anyone guide me into how I could get all the products and only the first product of the grouped ones. Hope I'm clear enough

Thanks
Feb 25 '08 #1
5 2653
ronverdonk
4,258 Expert 4TB
Something like this will do it (I hope)
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table_name WHERE color_group>'' GROUP BY color_group
  2.   UNION
  3. SELECT * FROM table_name WHERE color_group='' GROUP BY color;
Ronald
Feb 25 '08 #2
pechar
56
Something like this will do it (I hope)
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table_name WHERE color_group>'' GROUP BY color_group
  2.   UNION
  3. SELECT * FROM table_name WHERE color_group='' GROUP BY color;
Ronald
I did as you told me and used the following where alt_code is the productgroup:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM stocks WHERE alt_code >'' GROUP BY alt_code  
  2. UNION
  3. SELECT * FROM stocks WHERE alt_code='' GROUP BY alt_code
But the result is still the same. I get grouped products with a product group (as i want it) but also a group of the remaining products with no product group specified. I want these to be displayed not in a group but as single records (since these are all unique products)

To make is simpler what I need is e.g a cap in a group since I can have multicoloured, multisized caps. and a watch (which has no group) as a single product. In my case I have around 5000 products of which 500 are in a group so the result should give me something above 4500 say 4600 rows.

Thanks
Feb 26 '08 #3
ronverdonk
4,258 Expert 4TB
You have NOT implemented what I advised you to do, i.e.

The first set is grouped by the product group.
The second set is grouped by the color

the code you use groups everything by color group (alt_code). I'll show my code again
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table_name WHERE color_group>'' GROUP BY color_group
  2.   UNION
  3. SELECT * FROM table_name WHERE color_group='' GROUP BY color;
  4.  
Ronald
Feb 26 '08 #4
pechar
56
You have NOT implemented what I advised you to do, i.e.

The first set is grouped by the product group.
The second set is grouped by the color

the code you use groups everything by color group (alt_code). I'll show my code again
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table_name WHERE color_group>'' GROUP BY color_group
  2.   UNION
  3. SELECT * FROM table_name WHERE color_group='' GROUP BY color;
  4.  
Ronald
Oh sorry Ronald,

I missed that. But thanks a lot for the help much appreciated. Problem solved like this :)

Lukas
Feb 27 '08 #5
ronverdonk
4,258 Expert 4TB
Glad to be of help. See you around next time.

Ronald
Feb 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
2
by: Tarbuza Smith | last post by:
I was told that it is easy to create a table in MYSQL but it is hard to create a table with relationship like in Oracle or SQL Server by using Relationship or Query by Design or whatever wysiwyg...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
10
by: smorrey | last post by:
Hello all, this might better be suited for the MySQL newsgroup, but I figured I'ld post here and see if anyone can help me. I'm trying to create a simple transaction handling system where users...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
27
by: gerrymcc | last post by:
Hello, I'm a php/mysql beginner... Is there any way of making the mysql command line client full-screen? Sometimes it's easier to use the client than go thru php, but since it's only about 80...
5
by: ScottCase | last post by:
Hello, I have this sql statement and it works fine with mysql v4 $query = "SELECT id, name, jv_signups, mem_signups, ( SELECT SUM( jv_signups ) ) + ( SELECT SUM( mem_signups ) ) AS total...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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
Oralloy
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.