Recent

Author Topic: Doubt with SQL query  (Read 3349 times)

Giu

  • Full Member
  • ***
  • Posts: 131
Doubt with SQL query
« on: June 26, 2011, 01:20:41 pm »
Hi all, I have a little doubt about the construction of a SQL Query.
I have a Table ART (with id and name fields), a table DESCR (with id and name) and a table ARTDESCR (with artid and descrid).

Table Art looks like (Data is irrelevant)
1   Edit1                                             
2   Edit2                                             
3   Edit3                                             
4   Edit4                                             
5   Edit5                                             
6   Edit6                                             
7   Edit7                                             

Table Descr, looks like

1   Archivistica                                     
2   Documentación                                   
3   Achilipu                                         
4   Grome nagüer                                     
5   Edit6                                             

And table ArtDescr looks like (ARTID, DESCRID)

1   1
1   2
2   1
2   4
3   3
4   1
4   2
5   1
5   2
5   4
7   1
7   3


I need, to get the ARTS, having a ARTDESCR record with DescrID equal to 1 or 2, for example
Code: [Select]
SELECT Articulos.NombreArt FROM ARTICULOS, ARTDESCR
where (Articulos.ARTID=ARTDESCR.ARTID and (ARTDESCR.DESCRID = 1 OR ARTDESCR.DESCRID = 2))
group by NombreArt

This works, and it's ok, but, I'm having problems with the next "option"
I need, to get the ARTS, who has ARTDESCR records with DescrID equal to 1 AND 2
This means, it's suppose to get only, Edit1, Edit4 and Edit5, but I can't get the right SQL to get it, and this does'nt works

Code: [Select]
SELECT Articulos.NombreArt FROM ARTICULOS, ARTDESCR
where (Articulos.ARTID=ARTDESCR.ARTID and (ARTDESCR.DESCRID = 1 AND ARTDESCR.DESCRID = 2))
group by NombreArt

A lot of time away from SQL.  :-\

Giu

  • Full Member
  • ***
  • Posts: 131
Re: Doubt with SQL query
« Reply #1 on: June 26, 2011, 01:46:54 pm »
Ok, seems like I start to remember

Code: [Select]
select Articulos.NombreArt from ARTICULOS
where articulos.ARTID IN (select artid from ARTDESCR where ARTDESCR.DESCRID=1)
and articulos.ARTID IN (select artid from ARTDESCR where ARTDESCR.DESCRID=2)

Sorry for the noise.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Doubt with SQL query
« Reply #2 on: June 26, 2011, 02:06:41 pm »
optimized:
Code: [Select]
select Articulos.NombreArt from Aarticulos artc
join Artdescr artd on artc.ArtID = artd.ArtID and artd.DescrID in (1,2)

 

TinyPortal © 2005-2018