Link to home
Start Free TrialLog in
Avatar of Dave IT
Dave IT

asked on

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
Avatar of ste5an
ste5an
Flag of Germany image

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.
Avatar of Vijaya Kumar
Vijaya Kumar

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)
-------------------------------------------------------------------------------------------------------------------------User generated imagewhere org was first table,org2 was second table
Avatar of Dave IT

ASKER

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...
Simply normalize your data.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.