|
I believe your first ELSE should be an ELSEIF (or ELSE IF - can't remember exact syntax)
|
# ? Jun 9, 2008 17:10 |
|
|
# ? May 24, 2024 22:40 |
|
I have a table that holds data like this:code:
code:
|
# ? Jun 10, 2008 04:22 |
|
select max(score) from table group by name order by name, untested
|
# ? Jun 10, 2008 04:39 |
|
Thanks, that was a bit of a moment for me.
|
# ? Jun 10, 2008 05:01 |
|
I have a problem somewhat similar to the guy above me.code:
code:
code:
RDBMS is MySQL 5.0, if there is something 5.1 gives over 5.0 I would be willing to upgrade.
|
# ? Jun 10, 2008 05:05 |
|
After some further tinkering it seems my early example wasn't very good, and the problem is a bit more complicated. Let's say I have a burger designer software with 2 tables, burger and burger_ingredients. It allows me to create different variations of burgers and compare how successful they are.code:
code:
|
# ? Jun 10, 2008 08:06 |
|
Maybe MySQL doesn't allow inline views inside your literal views? Maybe you can create that subquery as another view and reference it that way... Seems silly though.
|
# ? Jun 10, 2008 13:00 |
|
MySQL does not support subqueries in the from clause within a view http://dev.mysql.com/doc/refman/5.0/en/create-view.html
|
# ? Jun 10, 2008 13:34 |
|
I'm trying to create a Trigger that allows only one bit within a column to be true. This is painfully simple but I haven't worked with Triggers before; is there a good guide out there and could someone help me with this specific one? The thing I'm running in to, mainly, is how do I grab the id of the row which had it's bit was changed so I can set the rest to false? I have this right now code:
|
# ? Jun 10, 2008 23:03 |
|
yaoi prophet posted:Maybe MySQL doesn't allow inline views inside your literal views? Maybe you can create that subquery as another view and reference it that way... Seems silly though. I'm going to do this unless I come across a better solution. Thanks!
|
# ? Jun 10, 2008 23:32 |
|
insidion posted:I'm trying to create a Trigger that allows only one bit within a column to be true. I'm not familiar with triggers, but when you write the logic to check if a single bit is set this page will probably be useful: http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#DetermineIfPowerOf2
|
# ? Jun 10, 2008 23:49 |
|
Aredna posted:I'm not familiar with triggers, but when you write the logic to check if a single bit is set this page will probably be useful: Thanks, it's a column of SQL Bits though which is essentially a boolean not a field of bits like you're thinking. I'm fairly solid with bit fields, but I'll keep that around for brushing up I found this too for anyone else who wants to figure out triggers: http://www.devarticles.com/c/a/SQL-Server/Using-Triggers-In-MS-SQL-Server/1/ That pretty much solved it for me.
|
# ? Jun 11, 2008 00:16 |
|
I'm trying to duplicate the 'refined search' feature that eBay uses on its search pages. It will display your list of searched results, and then suggest other searches with their result counts along the side. Doing this is easy, but doing it efficiently doesn't appear to be simple. Obviously I can do a query for each result count to my database based on the user's search criteria, but that means I have to query my entire table once for each search refinement option. Is there any way to do this in MySQL without querying the table 20+ times?
|
# ? Jun 11, 2008 16:43 |
|
Stephen posted:I'm trying to duplicate the 'refined search' feature that eBay uses on its search pages. It will display your list of searched results, and then suggest other searches with their result counts along the side. I'd try storing results in temp tables and querying those: I search for X: store in #tableX Refine search X+Y: search #tableX for Y: store in #tableXY Refine search X+Y+Z: search #tableXY for z: store in #tableXYZ Then just do a count on the temp tables and destroy them. That way, your first search might return, say, 500 records out of 50k. Your next searches are only searching those 500 records, or less (if you are adding refined search results like X + Y + Z and not just, X + Y or X + Z, or X + A, etc.)
|
# ? Jun 11, 2008 17:35 |
|
Anyone have suggestion for a way to mimic MINUS in mysql? I frequently need to diff two result sets and nothing I have found online is usable in a production environment due to the immense amount of time it seems to take.
|
# ? Jun 11, 2008 17:48 |
|
Puddleboy posted:I'd try storing results in temp tables and querying those: Is querying temp tables generally faster or is it just because there are less rows to query each time?
|
# ? Jun 11, 2008 17:53 |
|
Stephen posted:This is a great idea! Thanks. The only reason this is faster is because of the fewer number of rows. A standard table can be searched much faster if you index it, than a same-data temporary table (as far as I know).
|
# ? Jun 11, 2008 18:14 |
|
I've got another question, unfortunately. Table: items Columns: id, price Short of hard-coding the values, what would be the easiest way in MySQL to get a count of all the number of items in any price range? For example: 0-10000 (30 results) 10001-20000 (43 results) 20001-30000 (33 results) etc...
|
# ? Jun 11, 2008 22:42 |
|
Stephen posted:I've got another question, unfortunately. Something like this, you may need to play with those numbers a bit to make it so your price_range looks correct code:
|
# ? Jun 12, 2008 14:22 |
|
Begby posted:Something like this, you may need to play with those numbers a bit to make it so your price_range looks correct Thanks, that worked perfectly! I've never used the FLOOR or CEILING functions so I would've never thought of that.
|
# ? Jun 12, 2008 15:22 |
|
Stephen posted:Thanks, that worked perfectly! I've never used the FLOOR or CEILING functions so I would've never thought of that. I never thought of that either. You need to rely on geniuses to think of this crap. You should check out 'MySQL Cookbook' from oreilly press. There are a poo poo ton of really good solutions in there for these kinds of things, thats where I found this solution in about 2 minutes of thumbing through pages.
|
# ? Jun 12, 2008 16:30 |
|
Stephen posted:I've got another question, unfortunately. CASE is also nice for mapping values into buckets, which is powerful with GROUP BY. This is helpful when you need more complexity than a simple math formula will provide: code:
|
# ? Jun 12, 2008 17:13 |
|
double post
|
# ? Jun 12, 2008 17:13 |
|
fansipans posted:CASE is also nice for mapping values into buckets, which is powerful with GROUP BY. This is helpful when you need more complexity than a simple math formula will provide: Wow, extremely cool usage! I have absolutly no use for this right now, but I will allow it to percolate in my brain... Thanks!
|
# ? Jun 12, 2008 19:28 |
|
code:
|
# ? Jun 13, 2008 19:26 |
|
Stephen posted:
The query is trying to return, for each make/model, the make, the model, the number of times it has been viewed, one photo, and something from auto_upsales?
|
# ? Jun 13, 2008 19:45 |
|
Right off you can change this:code:
code:
What fields are indexed in each of the tables? Do you need left joins or is the database set up so that if autos.make_id or autos.model_id is in the autos table then makes.id and models.id will exist? Can you post the execution plan? That may give the most insight into being able to help optimize the query.
|
# ? Jun 13, 2008 19:48 |
|
Stephen posted:
There's absolutely no reason to include an ORDER BY in a sub-select, you don't need them orderd. All you're doing is causing extra work for the database. Also, avoid SELECT *, just request the fields you need.
|
# ? Jun 13, 2008 19:55 |
|
Aredna posted:Right off you can change this: The GROUP BY will still force the query to return only one row per auto_id in the photos table. The ORDER BY clause in the original sub-select will probably make it return the row for the auto_id having the largest or smallest orderid. Taking this out of the sub-select will probably cripple performance, doing an outer join against a 200k row table. You gotta feel bad for MySQL users. Without having analytics you really have to bend over backwards to avoid self joins.
|
# ? Jun 13, 2008 19:58 |
|
var1ety posted:The GROUP BY will still force the query to return only one row per auto_id in the photos table. The ORDER BY clause in the original sub-select will probably make it return the row for the auto_id having the largest or smallest orderid. Taking this out of the sub-select will probably cripple performance, doing an outer join against a 200k row table. When I remove the JOINs on the views and photos table the query is done in 0.001 seconds, so I know where the problem is, but unfortunately I don't know any really good way to fix it.
|
# ? Jun 13, 2008 21:19 |
|
Well one thing I did that shaved a full second off the query was to remove the JOIN on the photos table and just query for each individual photo within a loop in my PHP. The three queries add up to about 1.5 seconds now instead of 2.2.
|
# ? Jun 13, 2008 21:23 |
|
var1ety posted:The GROUP BY will still force the query to return only one row per auto_id in the photos table. The ORDER BY clause in the original sub-select will probably make it return the row for the auto_id having the largest or smallest orderid. Taking this out of the sub-select will probably cripple performance, doing an outer join against a 200k row table. Ahh, my understanding was that if you just groupped by one field, and the other 2 fields would return a data for every row in the table. Is this specific to MySQL or did I just completely misunderstand group by?
|
# ? Jun 13, 2008 21:23 |
|
var1ety posted:You gotta feel bad for MySQL users. Without having analytics you really have to bend over backwards to avoid self joins.
|
# ? Jun 13, 2008 21:23 |
|
Aredna posted:Ahh, my understanding was that if you just groupped by one field, and the other 2 fields would return a data for every row in the table. Is this specific to MySQL or did I just completely misunderstand group by? This is something that MySQL handles wrong. In most database systems, if you try to access columns that are not explicitly included in your GROUP BY, it will complain. For example, in Oracle: code:
In MySQL, you can refer to other columns not included in the GROUP BY criteria plainly. Of course the question is, which row are you getting back, and I think the answer is "whichever", but I haven't read any actual MySQL documentation on this subject in years so it may have changed.
|
# ? Jun 13, 2008 21:44 |
|
yaoi prophet posted:In MySQL, you can refer to other columns not included in the GROUP BY criteria plainly. Of course the question is, which row are you getting back, and I think the answer is "whichever", but I haven't read any actual MySQL documentation on this subject in years so it may have changed. I found that MySQL just returns the last row in whatever order it groups it by, usually the primary key. So in my case I had to sort it by the orderid, and then group it in order to return one row with the result I wanted.
|
# ? Jun 13, 2008 21:50 |
|
Stephen posted:I found that MySQL just returns the last row in whatever order it groups it by, usually the primary key. So in my case I had to sort it by the orderid, and then group it in order to return one row with the result I wanted. The concern here is that it's undefined behavior, and it may appear to work in many circumstances but not in others. A good example from Oracle is where people order by ROWID, hoping to preserve the "natural" ordering of the table. I see this a lot and it's bad because it usually looks like it's working, but it will occasionally not -- Oracle uses ROWID internally and makes no guarantee about table data being in any particular order. This is the same sort of situation. It would be important to know what the explicit behavior should be, but a quick check of the MySQL docs about GROUP BY doesn't show any mention of this behavior that I can find.
|
# ? Jun 13, 2008 21:57 |
|
yaoi prophet posted:The concern here is that it's undefined behavior, and it may appear to work in many circumstances but not in others. A good example from Oracle is where people order by ROWID, hoping to preserve the "natural" ordering of the table. I see this a lot and it's bad because it usually looks like it's working, but it will occasionally not -- Oracle uses ROWID internally and makes no guarantee about table data being in any particular order. How do you get around something like this in Oracle? If you aren't allowed to reference columns that aren't in the GROUP BY, how do you output them with the rest of the row? Is this a MySQL habit that I should kick right now before I get into Oracle/SQLServer?
|
# ? Jun 13, 2008 22:06 |
|
Stephen posted:How do you get around something like this in Oracle? If you aren't allowed to reference columns that aren't in the GROUP BY, how do you output them with the rest of the row? Is this a MySQL habit that I should kick right now before I get into Oracle/SQLServer? In older versions you would need to self join (get the id and maximum value and then join back to the original table). In newer versions you can use analytics such as row_number() or rank() to compute an ordering which you then filter on.
|
# ? Jun 13, 2008 22:22 |
|
yaoi prophet posted:This means you always, always have to include all columns as either GROUP BY criteria, or wrapped in an aggregate function in some way.
|
# ? Jun 14, 2008 10:14 |
|
|
# ? May 24, 2024 22:40 |
|
This is a dumb question, but I've never had it come up before: In MSSQL is there a way to do set @agg = (select sum(booya) from table1) + (select sum(booya) from table2) instead of doing it in two steps? I don't think you can cast each side to the correct datatype or whatever.
|
# ? Jun 14, 2008 18:53 |