How to use mysql statement to select and display only

Dave IT
Dave IT used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.
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 IT

Author

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
then you need to write procedure...
ste5anSenior Developer

Commented:
Simply normalize your data.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial