SQLTeam.com | Weblogs | Forums

Help with SQL code


#1

Hi there,

i have this table

i run this query:

SELECT *
FROM pippo
WHERE B like 'XX'
UNION
SELECT *
FROM pippo
WHERE E like 'XX'

because i want only the record that contain "XX" (the column A,B,C,D,E,F must remain) but the query output return even a record with "YY"!

Can someone help me???

Thanks


#3

This

Thanks


#5

Ok this is a great method but my table was an example, i have in a real world a table with 100 or more records and i need a quick method to search "XX" or something like this. I can't create every time a new table and put NULL in every record!


#7

ok thanks i try to explain better

in this table column A,G,D,L are equal;

if i want to search "XX" as output only the records that contain "XX"

i hope i was clear!

Thanks a lot!


#8

you could write something like:

select * from pippo where 'xx' in (a,b,c,d,e)


#10

I obtain the same output and the record with "YY" remains!


#11
select * from pippo where 'xx' in (a,b,c,d,e)
except 
select * from pippo where 'yy' in (a,b,c,d,e)

#12

This is not clear...

It sounds like you want to associate columns A,B,C as one record - then D,E,F as a second record, G,H,{D?} as a third record and L,M,N as a fourth record.

If you really have a table setup with 'repeating' segments of 'records', you really need to reconsider the design.

To get what I think you are asking:

SELECT A, B, C FROM pippo WHERE B = 'XX'
UNION ALL
SELECT D,E,F FROM pippo WHERE E = 'XX'
UNION ALL
SELECT G,H,{D?} FROM pippo WHERE H = 'XX'
UNION ALL
SELECT L,M,N FROM pippo WHERE M = 'XX'

If this is correct - you really have to redesign your 'table' and normalize the data.


#13

Yes i arrived at the same point. Thnaks for your help