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 > Reusing subqueries
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Reusing subqueries

Réponse
 
LinkBack Outils de la discussion
Vieux 12/10/2007, 11h13   #1
lister
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Reusing subqueries

Hi all,

I am trying to weight results of various subqueries and amalgamate
them into a single result set.

I can't rely on UNION to remove duplicate rows since I am manually
attributing different scores to each subquery, so they are not
duplicate. Instead I am having to add an additional NOT IN clause
which lists the results of the subqueries already executed.

This seems very ineffecient to me, and just wondered if there was any
way of telling MySql to just reuse the results of the subequery that's
already executed. I've tried assigning an alias to each subquery but
it tells me my query syntax is wrong.

Example of the kind of thing I want to do:

SELECT (SELECT *, 100 as score FROM table WHERE [complexsubquery1] AS
sub1)
UNION ALL
SELECT (SELECT *, 50 as score FROM table WHERE [complexsubquery2] AND
id NOT IN (sub1.id) AS sub2 )
UNION ALL
SELECT (SELECT *, somecol * 2 as score FROM table WHERE
[complexsubquery3] AND id NOT IN (sub1.id UNION sub2.id)
ORDER BY score

Can anyone tell me if this is possible and I just have the syntax
wrong, or is there some other way to accomplish what I want?

I'm using MySQL 5.0 if that makes any difference.

Many thanks for any

Lister

  Réponse avec citation
Vieux 12/10/2007, 11h20   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reusing subqueries

On 12 Oct, 10:13, lister <listerofsme...@hotmail.com> wrote:
> Hi all,
>
> I am trying to weight results of various subqueries and amalgamate
> them into a single result set.
>
> I can't rely on UNION to remove duplicate rows since I am manually
> attributing different scores to each subquery, so they are not
> duplicate. Instead I am having to add an additional NOT IN clause
> which lists the results of the subqueries already executed.
>
> This seems very ineffecient to me, and just wondered if there was any
> way of telling MySql to just reuse the results of the subequery that's
> already executed. I've tried assigning an alias to each subquery but
> it tells me my query syntax is wrong.
>
> Example of the kind of thing I want to do:
>
> SELECT (SELECT *, 100 as score FROM table WHERE [complexsubquery1] AS
> sub1)
> UNION ALL
> SELECT (SELECT *, 50 as score FROM table WHERE [complexsubquery2] AND
> id NOT IN (sub1.id) AS sub2 )
> UNION ALL
> SELECT (SELECT *, somecol * 2 as score FROM table WHERE
> [complexsubquery3] AND id NOT IN (sub1.id UNION sub2.id)
> ORDER BY score
>
> Can anyone tell me if this is possible and I just have the syntax
> wrong, or is there some other way to accomplish what I want?
>
> I'm using MySQL 5.0 if that makes any difference.
>
> Many thanks for any
>
> Lister


You could create temoporary tables.

  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 00h51.


É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,08899 seconds with 10 queries