PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Problem with INSERT INTO ... SELECT with HAVING clause
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with INSERT INTO ... SELECT with HAVING clause

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 20h35   #1
PleegWat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with INSERT INTO ... SELECT with HAVING clause

Hi,

I've got a problem with a complicated INSERT INTO ... SELECT statement.
It works correctly in mysql 5.0.45, but I've had complaints it doesn't
work in 4.1, and according to the documentation it shouldn't work in any
version.

The issue, is that the columns `stages`.`count`, referenced in the HAVING
clause of the SELECT statement, is not in the SELECT list. However, I
can't add it there, because there is no column to insert it into. Any
options I'm overlooking?

SQL Creates:

CREATE TABLE `roster_items` (
`member_id` int(11) unsigned NOT NULL default '0',
`item_name` varchar(96) NOT NULL default '',
`item_parent` varchar(64) NOT NULL default '',
`item_slot` varchar(32) NOT NULL default '',
`item_color` varchar(16) NOT NULL default '',
`item_id` varchar(64) default NULL,
`item_texture` varchar(64) NOT NULL default '',
`item_quantity` int(11) default NULL,
`item_tooltip` mediumtext NOT NULL,
`level` int(11) default NULL,
`item_level` int(11) default NULL,
`locale` varchar(4) default NULL,
PRIMARY KEY (`member_id`,`item_parent`,`item_slot`),
KEY `parent` (`item_parent`),
KEY `slot` (`item_slot`),
KEY `name` (`item_name`) `member_id` int(11) NOT
NULL DEFAULT 0,
`key_name` varchar(16) NOT NULL DEFAULT '',
`stage` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`member_id`, `key_name`, `stage`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `roster_addons_keys_stages` (
`faction` char(1) NOT NULL DEFAULT '',
`key_name` varchar(16) NOT NULL DEFAULT '',
`stage` int(11) NOT NULL DEFAULT 0,
`type` char(2) NOT NULL DEFAULT '',
`value` varchar(128) NOT NULL DEFAULT '',
`count` int(11) NOT NULL DEFAULT 0,
`flow` char(2) NOT NULL DEFAULT '',
`active` int(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`faction`, `key_name`, `stage`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `roster_addons_keys_keycache` (
`member_id` int(11) NOT NULL DEFAULT 0,
`key_name` varchar(16) NOT NULL DEFAULT '',
`stage` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`member_id`, `key_name`, `stage`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

SQL Query:

INSERT INTO `roster_addons_keys_keycache`
(`member_id`, `key_name`, `stage`)
SELECT '47', `stages`.`key_name`, `stages`.`stage`
FROM `roster_addons_keys_stages` AS stages, `roster_items` AS data
WHERE `stages`.`faction` = 'H'
AND `data`.`member_id` = '47'
AND
(`stages`.`type` = 'In'
AND `data`.`item_name` = `stages`.`value`
OR `stages`.`type` = 'Ii'
AND `data`.`item_id` LIKE CONCAT(`stages`.`value`, ':%')
)
GROUP BY `stages`.`key_name`, `stages`.`stage`, `stages`.`count`
HAVING SUM(`data`.`item_quantity`) >= `stages`.`count`;


`keycache` is an internal cache table that is updated on a data update,
to save work and complexity on display. '47' is a literal filled in by
PHP code, depending on what bit of data got updated.

--
PleegWat
Remove caps to reply
  Réponse avec citation
Vieux 07/02/2008, 12h14   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with INSERT INTO ... SELECT with HAVING clause

On 6 Feb, 20:35, PleegWat
<pleegwat.REM...@CAPS.leegwater-68.nl.INVALID> wrote:
> Hi,
>
> I've got a problem with a complicated INSERT INTO ... SELECT statement.
> It works correctly in mysql 5.0.45, but I've had complaints it doesn't
> work in 4.1, and according to the documentation it shouldn't work in any
> version.
> Any
> options I'm overlooking?


One option that you're overlooking is to tell us more than "it doesn't
work". What is the precise error that is being observed?

You also say: "according to the documentation it shouldn't work in any
version.". How about pointing us to the precise piece of documentation
that says that?
  Réponse avec citation
Vieux 07/02/2008, 20h28   #3
PleegWat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with INSERT INTO ... SELECT with HAVING clause

On Thu, 07 Feb 2008 04:14:51 -0800, Captain Paralytic wrote:

> On 6 Feb, 20:35, PleegWat
> <pleegwat.REM...@CAPS.leegwater-68.nl.INVALID> wrote:
>> Hi,
>>
>> I've got a problem with a complicated INSERT INTO ... SELECT statement.
>> It works correctly in mysql 5.0.45, but I've had complaints it doesn't
>> work in 4.1, and according to the documentation it shouldn't work in
>> any version.
>> Any
>> options I'm overlooking?

>
> One option that you're overlooking is to tell us more than "it doesn't
> work". What is the precise error that is being observed?
>
> You also say: "according to the documentation it shouldn't work in any
> version.". How about pointing us to the precise piece of documentation
> that says that?


Ah, right. Sorry.

Error message is:

| 1054: Unknown column 'stages.count' in 'having clause'

The MySQL 4.1 documentation (http://dev.mysql.com/doc/refman/4.1/en/
select.html) states:

| A HAVING clause can refer to any column or alias named in a
| select_expr in the SELECT list or in outer subqueries, and to
| aggregate functions. (Standard SQL requires that HAVING must reference
| only columns in the GROUP BY clause or columns used in aggregate
| functions.)

MySQL 5.0 documentation (http://dev.mysql.com/doc/refman/5.0/en/
select.html) states:

| A HAVING clause can refer to any column or alias named in a
| select_expr in the SELECT list or in outer subqueries, and to
| aggregate functions. However, the SQL standard requires that HAVING
| must reference only columns in the GROUP BY clause or columns used in
| aggregate functions. To accommodate both standard SQL and the MySQL
| -specific behavior of being able to refer columns in the SELECT list,
| MySQL 5.0.2 and up allows HAVING to refer to columns in the SELECT
| list, columns in the GROUP BY clause, columns in outer subqueries, and
| to aggregate functions.

Which, now that I reread it, is consistent with the functionality I'm
seeing, since the problematic column is in the group by clause.


--
PleegWat\nRemove caps to reply
  Réponse avec citation
Vieux 07/02/2008, 21h40   #4
PleegWat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with INSERT INTO ... SELECT with HAVING clause

On Thu, 07 Feb 2008 20:28:57 +0000, PleegWat wrote:

> On Thu, 07 Feb 2008 04:14:51 -0800, Captain Paralytic wrote:
>
>> On 6 Feb, 20:35, PleegWat
>> <pleegwat.REM...@CAPS.leegwater-68.nl.INVALID> wrote:
>>> Hi,
>>>
>>> I've got a problem with a complicated INSERT INTO ... SELECT
>>> statement. It works correctly in mysql 5.0.45, but I've had complaints
>>> it doesn't work in 4.1, and according to the documentation it
>>> shouldn't work in any version.
>>> Any
>>> options I'm overlooking?

>>
>> One option that you're overlooking is to tell us more than "it doesn't
>> work". What is the precise error that is being observed?
>>
>> You also say: "according to the documentation it shouldn't work in any
>> version.". How about pointing us to the precise piece of documentation
>> that says that?

>
> Ah, right. Sorry.
>
> Error message is:
>
> | 1054: Unknown column 'stages.count' in 'having clause'
>
> The MySQL 4.1 documentation (http://dev.mysql.com/doc/refman/4.1/en/
> select.html) states:
>
> | A HAVING clause can refer to any column or alias named in a |
> select_expr in the SELECT list or in outer subqueries, and to |
> aggregate functions. (Standard SQL requires that HAVING must reference |
> only columns in the GROUP BY clause or columns used in aggregate |
> functions.)
>
> MySQL 5.0 documentation (http://dev.mysql.com/doc/refman/5.0/en/
> select.html) states:
>
> | A HAVING clause can refer to any column or alias named in a |
> select_expr in the SELECT list or in outer subqueries, and to |
> aggregate functions. However, the SQL standard requires that HAVING |
> must reference only columns in the GROUP BY clause or columns used in |
> aggregate functions. To accommodate both standard SQL and the MySQL |
> -specific behavior of being able to refer columns in the SELECT list, |
> MySQL 5.0.2 and up allows HAVING to refer to columns in the SELECT |
> list, columns in the GROUP BY clause, columns in outer subqueries, and |
> to aggregate functions.
>
> Which, now that I reread it, is consistent with the functionality I'm
> seeing, since the problematic column is in the group by clause.


OK, I've found something that works:

I've replaced `roster_addons_keys_stages` in the FROM clause by the
subquery:

SELECT `faction`,`key_name`,`stage`,`type`,`value`,`count `
FROM `roster_addons_keys_stages`

--
PleegWat
Remove caps to reply
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14375 seconds with 12 queries