How to use mysql statement to select and display only

First select the range of cc code when cc code >80000 and cc code <90000 , then replace the cc code when the cc code is on the range but the cc code is based on the cc code  from table 2 then loop it based on the count column and only display cc code 1 and cc code 2. So how the mysql selection for this?

Picture 1 is my table 1
Picture 2 is my table 2
Picture 2 is my desire output
Picture_1.PNG
Picture_2.PNG
Picture_3.PNG
Dave ITnewbie ITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Problem 1: Please post concise and complete code in future posts. Images are not optimal, at least embed them instead of attaching them.
Problem 2: Table 2 is not normalized.

E.g.

SELECT  T1.[Item No.] ,
        CC.[CC_Code] ,
        T1.[SYS Name] ,
        T1.[Day]
FROM    Table1 T1 ,
(   SELECT T2.[CC Code 1] AS [CC_Code]
    FROM   Table2 T2
    WHERE  T1.[CC Code] = T2.[CC Code] ) CC
WHERE   T1.[CC Code]
BETWEEN 80000 AND 90000
UNION ALL
SELECT  T1.[Item No.] ,
        CC.[CC_Code] ,
        T1.[SYS Name] ,
        T1.[Day]
FROM    Table1 T1 ,
(   SELECT T2.[CC Code 2] AS [CC_Code]
    FROM   Table2 T2
    WHERE  T1.[CC Code] = T2.[CC Code] ) CC
WHERE   T1.[CC Code]
BETWEEN 80000 AND 90000;

Open in new window


btw, don't use special characters and spaces in object names. This requires extensive use of object delimiters.
Vijaya KumarCommented:
your code look like this

(select o.sys_name,o.day,o2.cc_code1 from org2 o2 inner join org o on o.cc_code
between 80000 and 90000 and o2.c_code = o.cc_code)
union
(select o.sys_name,o.day,o2.cc_code2 from org2 o2 inner join org o on o.cc_code
between 80000 and 90000 and o2.c_code = o.cc_code)
-- on (o.cc_code > 80000 and o.cc_code < 90000)
union
(select o.sys_name,o.day,o2.cc_code3 from org2 o2 inner join org o on o.cc_code
between 80000 and 90000 and o2.c_code = o.cc_code)
-------------------------------------------------------------------------------------------------------------------------Mysql.PNGwhere org was first table,org2 was second table
Dave ITnewbie ITAuthor Commented:
if  i dont make it hard code can ? i need based on the `count` value to loop through the rc_code 1 , rc_code 2 until the `count` value end
Vijaya KumarCommented:
then you need to write procedure...
ste5anSenior DeveloperCommented:
Simply normalize your data.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.