MySQL query involving cross join, uniqueness, etc?

Through a bit of thinking real hard and Googling, I may have found a good solution: SELECT * FROM hams, eggs GROUP BY hams. Id, eggs. How_cooked It seems to work.Is it really that easy?

It works, yes, but only because you don't care which egg in particular. SQL behavior for selecting columns not in a GROUP BY isn't well-defined. However, in my experience, it always returns a valid value.

Also, this'll only work as long as eggs contains at least one occurrence of each value in the enum (of course, it looks like this is a pretty safe assumption). – cmptrgeekken Feb 2 '10 at 1:36 Good - I'm glad to know that there are, in fact, well-defined gotchas, since if there weren't any obvious ones, I'm sure obscure ones would show up. Thanks!

– Matchu Feb 2 '10 at 1:42 and mysql is afaik the only database that will allow you to do it this way – ysth Feb 2 '10 at 2:39 Which I believe is wrong, as it tempts you to believe the extraneous data actually relates to the aggregate data specifically. – RedFilter Feb 2 '10 at 3:28 Your query does not seem to work in MySQL 5.5 – Damon Feb 2 '10 at 5:15.

SELECT hams. Id, eggs. How_cooked, eggs.Id FROM hams CROSS JOIN eggs This does the trick.

CROSS JOIN is synonymous with , but has a higher precedence in MySQL . MySQL 5.0 Reference - JOIN syntax.

I have the table hams, which I would like to cross-join with the table eggs - that is, get all ham-egg combinations... to an extent. The eggs table also has an attribute how_cooked, which is defined as ENUM('over-easy','scrambled','poached'). I would like a resultset listing every possible combination of ham and egg-cooking method, along with a sample egg cooked that way.

I'm sure I could hack together some solution with loads of subqueries here and there, but is there any elegant way to do this is MySQL?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions