๐Ÿ’ SQL (Programmers)/Select

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][MySQL] ํŠน์ • ํ˜•์งˆ์„ ๊ฐ€์ง€๋Š” ๋Œ€์žฅ๊ท  ์ฐพ๊ธฐ (level1)

์„ ๋‹ฌ 2024. 11. 22. 14:27
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

2๋ฒˆ ํ˜•์งˆ์ด ๋ณด์œ ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ 1๋ฒˆ์ด๋‚˜ 3๋ฒˆ ํ˜•์งˆ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š”

๋Œ€์žฅ๊ท  ๊ฐœ์ฒด์˜ ์ˆ˜(COUNT)๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

1๋ฒˆ๊ณผ 3๋ฒˆ ํ˜•์งˆ์„ ๋ชจ๋‘ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๋„ 1๋ฒˆ์ด๋‚˜ 3๋ฒˆ ํ˜•์งˆ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์— ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

 

ํ’€์ด

n๋ฒˆ ํ˜•์งˆ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค = 2^(n-1) ์™€ & ์—ฐ์‚ฐ์„ ํ•˜๋ฉด 2^(n-1)๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

ex1. 1๋ฒˆ ํ˜•์งˆ์„ ๋ณด์œ ํ•˜๋ ค๋ฉด 2^(1-1) = 1 = 0001(2) ์™€ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•œ๋‹ค.

GENOTYPE = 11 = 1011(2) ๋ผ๋ฉด, 1011 & 0001 = 0001 -> ํ˜•์งˆ ๋ณด์œ  ํ•จ

GENOTYPE = 6 = 0110(2) ๋ผ๋ฉด, 0110 & 0001 = 0000 -> ํ˜•์งˆ ๋ณด์œ  ์•ˆํ•จ

 

ex2. 3๋ฒˆ ํ˜•์งˆ์„ ๋ณด์œ ํ•˜๋ ค๋ฉด 2^(3-1) = 4 = 0100(2) ์™€ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•œ๋‹ค.

GENOTYPE = 11 = 1011(2) ๋ผ๋ฉด, 1011 & 0100 = 0000 -> ํ˜•์งˆ ๋ณด์œ  ์•ˆํ•จ

GENOTYPE = 5 = 0101(2) ๋ผ๋ฉด, 0101 & 0100 = 0100 -> ํ˜•์งˆ ๋ณด์œ  ํ•จ

 

select count(*) as COUNT
from ECOLI_DATA
where (GENOTYPE&1=1 or GENOTYPE&4=4) and GENOTYPE&2!=2
๋ฐ˜์‘ํ˜•