Help with MySQLI WHERE clause

I am trying to retrieve records from a table of unique composition based on the following criteria:

-members belong to 2 different associations (coded: Association = 1 or 2)

-Some members have no postal addresses (coded: adresse_rue) and will not send mail.

-Some members Association = 1 don't you mail (coded: D or V or W, X or Z)

-No member of the Association 1 with cur_bal_2 = < gets 10 mail

-No member of the Association 2 with cur_bal_2 = <-1 get mail

-cur_bal_2 field is a numeric value in the table.

I tested

WHERE (Mbr_Scat = 'P' AND Association = 2 AND adresse_rue <>' ' AND cur_bal_2 > = - 1) by itself results accurate/w

I tested

WHERE (Mbr_Scat <>had ' AND Mbr_Scat <>'V' and 'W' AND Mbr_Scat <>'X' <>Mbr_Scat ' AND cur_bal_2 >-20 AND adresse_rue <>' ") by itself, results also w/accurate

But-

When I combine the above to:

WHERE

(Mbr_Scat = 'P' AND Association = 2 AND adresse_rue <>' ' AND cur_bal_2 > = - 1).

OR

(Mbr_Scat <>had ' AND Mbr_Scat <>'V' and 'W' AND Mbr_Scat <>'X' <>Mbr_Scat ' AND cur_bal_2 >-20 AND adresse_rue <>' ")

the part of the WHERE the criteria cur_bal_2 > = - 1 is not running (ONLY this part of the statement fails - members whose balances is less than-1 selecting.)

I get no error message.

Suggestions?

Best regards and thank you all,.

Tom Smith

Try this:

WHERE adresse_rue <> "

AND

((Association = 1 AND Mbr_Stat pas dans (serait ', 'V', 'W', 'X', 'Z') AND cur_bal_2 > = - 10) OR (Association = 2 AND cur_bal_2 > = - 1))

Tags: Dreamweaver

Similar Questions

  • Need help with dynamic where clause.

    Hello world.  I'm kind of a novice with PL/SQL, so please be patient with me.  I am building a web application using Weblogic Oracle 11 g and Oracle database.

    To make a long story short, I need to generate the complex dynamic WHERE clause.  I don't know what will be the name of the column or the operator of comparison or the value will be until the moment of execution.

    I'm not 100% certain, but I think I need to use dynamic SQL statements.  Unless there is a better way to do it.

    I'm sure that using dynamic SQL opens me up to SQL injection attacks if I use simple string concatenation.  I would like to use bind variable if possible.

    In any case, it seems like trying to build a dynamic where clause does not seem to work as I want it would be...

    bv1 := 'colA = 5 and colB >= 9';
    
    query_str := 'SELECT * FROM view WHERE :bv1';
    
    OPEN CUR FOR query_str USING bv1;
    

    The code above pours out when I try to run it.

    Is there a way to build dynamic where the provisions without the use of concatenation of simple string that leaves me open to attacks by SQL injection?

    Thank you

    Hello.  I have read briefly upward on things, you suggested, but it seems a bit abstract for me since I was a novice.

    Can you give us an explanation or an example of how to use it to accomplish what I need to do?

    Do ypu really plan create a query with a WHERE clause that contains a predicate for all possible columns?

    Variable bind offer performance when the same query is executed several times with different values for bind variables.

    Select * from emp where empno =: v_empno

    After the first run Oracle will always be this query in the library cache. The next running can use the version of the cache even if the bind value is different.

    Select * from emp where empno =: v_empno;

    Select * from emp where ename =: v_ename;

    Select * from emp where sal =: v_sal;

    Each one is a DIFFERENT query. Yes - each of them uses a variable binding, but queries are different.

    So, if your username select EMPNO, an '=', and a value from the first query can be executed. But the next time that the user can select the ENAME column so that 'stored' first request may not be used given that the query does not match.

    The third time your user selects SAL so none of the first two queries can be used in the library cache. The use of bind variables gives performance during the SAME QUERY is used multiple times with DIFFERENT values for the variable binding. Oracle is not to analyze the hard query everything simply because the VALUE is different.

    So bind variables do not help you if most of the queries were completely DIFFERENT sets of predicates. And SYS_CONTEXT will not help either since that simply provides a way to define binds the values of the variables.

    Just for the simple example above, there are at least SIX possible combinations of columns that could choose your user name: empno, ename, sal, empno ename, empno & sal, ename & sal

  • Help with the WHERE clause

    I have a Select statement where I want to select rows where weighs only 1 of the selected date 3 columns is null. Is this possible?

    Select rec_date, prior_rec_date2, prev_rec_date3
    of my_receipts
    where (not more than 1 or the date 3 fields is null)

    Any help would be greatly appreciated.

    Hello

    WHERE   NVL2 (rec_date,        1, 0) +
            NVL2 (prior_rec_date2, 1, 0) +
            NVL2 (prev_rec_date3,  1, 0) >= 2
    
  • How to create a procedure for REF CURSOR output with any WHERE clause?

    I have a requirement like this: I have a huge question that need to reuse in my code over 10 times. This SQL has about 50 lines. Here for these 10 odd times sometimes changes in WHERE clause (columns are the same). So I can't create a view as SQL is not static.

    I thought to write a procedure with a para of WHERE_CLAUSE entry. I pulled out a refcursor sys by adding where clause. But I can't do it because you cannot add a clause like this where clause.

    i.e.
    PROCEDURE dynamyic_query (p_where_clause IN VARCHAR2, p_out_query OUT SYS_REFCURSOR ) IS
    BEGIN
    
      OPEN p_out_query FOR SELECT ......... FROM table WHERE || ' ' || p_where_clause;
    
    END;
    The foregoing gives an error.

    How to handle such a situation? Any help would be greatly appreciated.

    Hello

    Channa wrote:
    ... However, when I change the procedure like this:

    PROCEDURE FORMS_TEXT_DYN_SQL_TEST(p_where_cluase IN VARCHAR2, p_out_cursor OUT SYS_REFCURSOR) IS
    v_stmt VARCHAR2(1000);
    BEGIN
    v_stmt := 'SELECT tname FROM tab_test WHERE tname LIKE ''%ABS_V4%'' AND :y';
    
    OPEN p_out_cursor FOR v_stmt using p_where_cluase;
    
    END;
    

    And run this block of code:

    declare
    v_tname varchar2(200);
    out_cursor sys_refcursor;
    begin
    forms_text_dyn_sql_test(' 1 = 1 ', out_cursor );
    LOOP
    fetch out_cursor INTO v_tname;
    exit when out_cursor%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(v_tname);
    END LOOP;
    end;
    /
    

    I get the error:

    [1]: (Error): ORA-00920: invalid relational operator ORA-06512: at "ABS.FORMS_TEXT_DYN_SQL_TEST", line 6 ORA-06512: at line 5
    

    Looks like you can only set column_name =: z, column_name =: values of type y. You can not it seems to replace it with no WHERE CLAUSE?

    A bind variable, such as: it, represents a single value.
    If: is the VARCHAR2 '1 = 1', then

    SELECT tname FROM tab_test WHERE tname LIKE '%ABS_V4%' AND :y
    

    takes the value

    SELECT tname FROM tab_test WHERE tname LIKE '%ABS_V4%' AND '1 = 1'
    

    I think you want something like this:

    CREATE OR REPLACE PROCEDURE FORMS_TEXT_DYN_SQL_TEST
    (     p_where_clause      IN      VARCHAR2
    ,      p_out_cursor      OUT      SYS_REFCURSOR
    ) IS
      v_stmt VARCHAR2(1000);
    BEGIN
      v_stmt := 'SELECT ename FROM scott.emp WHERE ename LIKE ''%A%'' AND '
              || p_where_clause;
    
      OPEN p_out_cursor FOR v_stmt;
    
    END;
    /
    show errors
    
    SET  SERVEROUTPUT  ON
    
    declare
      v_tname varchar2(200);
      out_cursor sys_refcursor;
    begin
      forms_text_dyn_sql_test(' 1 = 1 ', out_cursor );
      LOOP
        fetch out_cursor INTO v_tname;
        exit when out_cursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(v_tname);
      END LOOP;
    end;
    / 
    

    Output:

    ALLEN
    WARD
    MARTIN
    BLAKE
    CLARK
    ADAMS
    JAMES
    
  • 'create columns with a where clause' in MDB

    Hi all

    I have a question on the OBI EE meta data layer.

    We have OBIEE sitting on a with a star schema data warehouse.

    Our fact table contains the following fields...

    EmployeeID
    CompanyID
    ID of the measure
    The measure name
    MEASURE

    That's what we have in the physical layer, layer MDB and the presentation layer, but I want to be able to "create columns with a where clause.

    So my table of facts in the presentation layer would look like this

    Quantity sold
    Unit cost
    Amount of the company



    I thought I could just create a logical with column

    Quantity = measurement
    where as name = "quantity".

    But I can't find any to do so.

    Any ideas?


    Thank you

    Hello

    I think that what you want to achieve must be done in the physical layer and not the business layer.
    Instead of using your physical table directly from the DB, you must create a physical table based on an SQL statement.
    GO to the physical layer you / create the new object table in Table type choose "Select" and then you write your select statement as you want.
    Once this is done to create your physical joins and import your table into your business layer and create your complex joins
    This should do the trick.
    It may be useful

    Concerning

  • A CASE statement with additional where clause

    Hi all

    I need assistance in which this obligation clause

    Table:-balance_table

    Columns:-balance_type, balance_amount, balance_month, budget_name

    balance_type column had given 'Real' OR 'Budget '.

    Now I am trying to extract data such as: if: entered_month is Mar-2009 then before and including Mar-2009 will show real balance sheet Date and after Mar-2009 will show budget data.

    It works fine, I need to add a condition more restrict the budget name, there are several budget_name in the table for balance_type = 'Budget '.



    SELECT SUM (balance_amount), balance_type, balance_month
    OF balance_table
    WHERE balance_type =
    (CASE
    WHEN balance_month < =: entered_month
    THEN 'real '.
    ANOTHER 'Budget '.
    END
    )
    AND budget_name = 'BUDGET1.
    Balance_type GROUP, balance_month

    Stated above is erroneous because ' AND budget_name = ' BUDGET1 "clause restricts the set of data."

    Please help in contrcuting where clause 1) to sort the data according to Budget/actual and 2) for specific budgets, so budget_type = Budget

    Thank you
    Bobin

    That should do it.

    SELECT SUM (balance_amount),
           balance_type,
           balance_month
    FROM balance_table
    WHERE (balance_type = 'Actual'
           and balance_month <= :entered_month
          )
    or (balance_type = 'Budget'
        and balance_month > :entered_month
        and budget_name = :entered_budget_name
       )
    GROUP BY balance_type,
             balance_month
    ORDER BY 2
    

    See you soon
    Sarma.

  • How to filter the result with a where clause clause?

    Hello

    I have a nice sql query. I would like to include a where clause with a joker clause.

    If the user selects for example a "AGENT_ID", then where clause would include the AGENT_ID, otherwise, every thing would appear.
    SELECT 
       "FICHE"."FICHE_ID" "FICHE_ID", 
       "FICHE"."AGENT_ID" "AGENT_ID", 
       "FICHE"."EQUIPE_ID" "EQUIPE_ID", 
       "FICHE"."DATE_ACTIVITE" "DATE_ACTIVITE", 
       "FICHE"."DATE_CREATION" "DATE_CREATION", 
       "FICHE"."TYPE_FICHE" "TYPE_FICHE", 
       "FICHE"."FICHE_CREE_PAR" "FICHE_CREE_PAR", 
       "FICHE"."TOTAL_HEURES" "TOTAL_HEURES", 
       "FICHE"."TOTAL_HEURES_EXCEPTIONNELLES" "TOTAL_HEURES_EXCEPTIONNELLES", 
       "FICHE"."DATE_MODIFICATION" "DATE_MODIFICATION"
    FROM 
       "FICHE"
    The WHERE clause:
    WHERE AGENT_ID = ' * ' if nothing is selected.

    WHERE AGENT_ID = "4290" If an agent ID is selected.

    Hope I'm clear that English is not my mother tongue.

    Thanks for you help.

    Christian

    Hello

    Try this

    WHERE "FICHE".AGENT_ID like NVL(:val, '%')
    

    see you soon

    VT

  • T/t with the WHERE clause using a list breaks

    Hello, all,.

    I have a project where I need to use a list as part of the WHERE clause for a t/t, and it is breaking.  I don't know what I'm doing wrong.

    For some pseudo-code, do I have a complete query of a database called 'people '.  A list of values for the t/t with the first query; the second query is my t/t:

    <cfquery name="getDIR" datasource="#request.THISDSN#">
        SELECT thisName dir FROM orgs WHERE thisID in (<cfqueryparam value="#form.org#" cfsqltype="cf_sql_varchar" list="yes" />)
    </cfquery>
    
    <cfquery name="people" database="people">
        SELECT colA, colB, colC, colD
        FROM people
        WHERE 1=1
            AND (
                div IN (<cfqueryparam value="#valueList(getDIR.dir)#" cfsqltype="varchar" list="yes" />)
                OR dir IN (<cfqueryparam value="#valueList(getDIR.dir)#" cfsqltype="varchar" list="yes" />)
                )
    </cfquery>
    
    
    

    I get the error message is "t/t syntax error: encountered" div IN \'thisValue\' ".»  Incorrect conditional expression, waiting for one of [as | null | between | in | comparison] condition. »

    Can support _not_ QoQ lists of WHERE clauses?

    V/r,

    ^_^

    SMH - so much... I just found.

    I forgot to give the lists in the second query bracket.


    (The pseudocode was manually typed, since my dev system is isolated from the internet, and I added the in my pseudo code when they were not in my original code.)

    * headdesk * headdesk * headdesk * headdesk *.

    V/r,

    ^_^

  • XMLTable with a WHERE clause?

    I have a table that looks like this race under Oracle 11.2.0.3.4:
    CREATE TABLE book_master OF XMLTYPE XMLTYPE COLUMN STORE AS SECUREFILE BINARY XML;
    .. and I'm trying to run a query like this:
    SELECT
        e.title,
        e.isbn_nbr,
        e.prod_id,
        
        e.ctry_code,
        e.mkt_nbr,
        
        e.invt_data
        
    FROM 
        book_master g,
        XmlTable(
        XmlNamespaces('http://www.mrbook.com/InventoryData/book' AS "bk",
                      'http://www.mrbook.com/InventoryData' AS "invtdata"),
          '/invtdata:INVENT_DATA'
        
        PASSING g.OBJECT_VALUE
        COLUMNS
           title PATH '@title',
           isbn_nbr VARCHAR2(64) PATH 'bk:BOOK_ID/bk:ISBN',
           prod_id VARCHAR2(64) PATH 'bk:BOOK_ID/bk:PROD_ID',
           
           ctry_code NUMBER PATH 'bk:BOOK_ID/bk:CTRY_CODE',
           mkt_nbr NUMBER PATH 'bk:BOOK_ID/bk:MKT_NBR',
           
           invt_data XmlType PATH '.'
    ) e
    
    WHERE
      e.isbn_nbr = ?
    He played really bad, and then I realized that he returned each row in the table for each iteration of the query. Is XMLExists the only way possible to return a single line, assuming that ISBN_NBR and PROD_ID are unique? Can the clause WHERE cannot be used as a discriminator at all?

    Published by: KnightOfBlueArmor on 29 April 2013 11:57 (added Oracle version)

    KnightOfBlueArmor wrote:
    He played really bad, and then I realized that he returned each row in the table for each iteration of the query. Is XMLExists the only way possible to return a single line, assuming that ISBN_NBR and PROD_ID are unique? Can the clause WHERE cannot be used as a discriminator at all?

    I don't know what you're asking.

    The WHERE clause filters the result correctly?
    I'm curious to see an example, if this is the case.

  • Need help with order by clause row_number() Fn

    with t as
    (
     select 123 id, 'brwr' lstatus from dual
     union all
     select 123 id, 'ca' lstatus from dual
     union all
     select 123 id, 'fac' lstatus from dual
     union all
     select 345 id, 'ca' lstatus from dual
     union all
     select 345 id, 'brwr' lstatus from dual
     union all
     select 567 id, 'brwr' lstatus from dual
     union all
     select 789 id, 'brwr' lstatus from dual
     union all
     select 1011 id, 'fac' lstatus from dual
    union all
     select 1011 id, 'xyz' lstatus from dual
    )select id,lstatus,row_number() over(partition by id order by lstatus)rw
     from t;
    Desired output
    id           lstatus       rn
    
    123         brwr         3
    123         ca          1
    123         fac         2
    345         ca          1
    345         brwr         2
    567         brwr         1
    789         brwr         1
    1011         fac        1
    1011  xyz         2
    That is, my first priority is to 'ca', then 'College '.

    I am aware that this can be done through the use of CASE within the order by clause
    but could not find the logic of the apt.

    Please help me.

    A solution just for data given to samples...

    with t as
    (
     select 123 id, 'brwr' lstatus from dual
     union all
     select 123 id, 'ca' lstatus from dual
     union all
     select 123 id, 'fac' lstatus from dual
     union all
     select 345 id, 'ca' lstatus from dual
     union all
     select 345 id, 'brwr' lstatus from dual
     union all
     select 567 id, 'brwr' lstatus from dual
     union all
     select 789 id, 'brwr' lstatus from dual
     union all
     select 1011 id, 'fac' lstatus from dual
    union all
     select 1011 id, 'xyz' lstatus from dual
    )select id,lstatus,row_number() over(partition by id order by (case  when lstatus <> 'brwr' THEN UPPER(lstatus) else lstatus end)) rw
     from t;
    

    Basically, it shows how to use the CASE statement. You can change based on your actual data.

    Concerning
    Arun

  • Problem with a Where Clause in report

    Hi all

    I have a simple report on the EMP table with a form of parameter to select employees of a particular or all departments.
    It works fine if I'm in one or all the departments in the report. The SQL code for the report is the following:

    SELECT
    EMPNO,
    ENAME,
    HIREDATE,
    SAL,
    DEPTNO
    Of
    EMP
    WHERE
    DEPTNO LIKE "%" | : P68_DEPTNO | » %'

    Now, I have added another element of setting labeled "category". If you select the category 'OFFICE', she must return employees of departments 10 and 20, if selected from category 'FIELD', he has to return employees to Department 30 and 40. But if you select the category ALL then he must return the employees of all departments.

    Anyone would be able to advise me on the correct approach to achieve the above?

    You can view the report by clicking on this link: http://apex.oracle.com/pls/otn/f?p=43368:68

    Thank you
    Zahid

    Published by: Zahid Khan on April 15, 2009 16:20

    Something like:

    SELECT
    EMPNO,
    ENAME,
    HIREDATE,
    SAL,
    DEPTNO
    Of
    EMP
    WHERE
    DEPTNO LIKE "%" | : P68_DEPTNO | » %'
    AND
    ((: P68_CATEGORY = 'OFFICE' AND deptno IN (10,20)))
    OR (: P68_CATEGORY = 'FIELD' AND deptno IN (30, 40))
    OR: P68_CATEGORY = "ALL".
    )

    But what happens if the user chooses the category OFFICE and Deptno 30? What needs to happen next? The query above will return any given - no one is in the two 30 and Department 10 or 20!

  • Several SELECT statements with COUNTY with different WHERE clauses.

    OK, so for a bit of background on what this is trying to accomplish...
    I have a small group of people in my team and I will use GROUP BY for their names.
    If each person will be on their own line. Next to their names, I want to be able to have several columns that use the ACCOUNT for the different search criteria (no duplicates)

    Overall, it should have their names, and I would like to add the number of tickets that each person has which meet certain criteria (all in different columns)
    I have applications that can do each of these tasks, but is it possible that I can group together on a single table statements, or use subqueries for these results?

    Do you mean you want something like that?

    SQL > ed
    A written file afiedt.buf

    1 with the team (emp_id, emp_name)
    2 (select 1, 'Fred' from dual union all
    3. Select 2, 'Bob' Union double all the
    4. Select 3, 'Jim' to double
    5            )
    6, tickets (emp_id, ticket_type, ticket_id, ticket_desc)
    7 (select 1, 1, 'BUG', 'First billet Freds' double Union all)
    8. Select 2, 1, 'CHANGE', "Freds second ticket" of all the double union
    9 select 3, 1, 'BUG', ' Freds third ticket "of any double union
    10. Select 4, 2, "CHANGE", "Bobs first ticket" of any double union
    11. Select 5, 3, "CHANGE", "Jims first ticket" of any double union
    12. Select 6, 3, "BUG", "Jims second ticket" of all the double union
    13. Select 7, 1, 'BUG', ' Freds fourth ticket ' from dual
    14            )
    15-
    16 END OF TEST DATA
    17-
    18 select emp_name
    19, count (case when ticket_type = 'BUG' then 1 end to another null) and bugs
    20, count (case when ticket_type = 'CHANGE' then 1 end to another null) as changes
    21 of the team t
    22 a left join external tickets tk (t.emp_id = tk.emp_id)
    23 * emp_name group
    SQL > /.

    EMP_ CHANGES OF BUGS
    ---- ---------- ----------
    Bob           0          1
    Fred 3 1
    Jim           1          1

  • Download data from an external table with a where clause clause

    Hello

    How can I insert data in an external table into a table already created in the database where person_id = person - _id on the external table?

    Example:

    External table
    XX_EXTERNAL_TBL (Person_id, emp_number, emp_name)
    Internal table
    XX_SQL_LOADER_TEST (Person_id, emp_number, emp_name)
    ID of the person already exists on the inner table (want only the last column to import where the ID of the person are the same)


    Thank you

    You talk of * 'Update' * (Fanfarrias y tambores)

    Update XX_SQL_LOADER_TEST SLT
    Set emp_number = (SELECT emp_number from XX_EXTERNAL_TBL WHERE AND.) Person_id = HI. PERSON_id)
    , emp_name = (SELECT XX_EXTERNAL_TBL emp_name AND where AND.) Person_id = HI. PERSON_id)

  • help with query - where the multiples in the Group column

    4 columns in the table named RRID1

    RRI_ID
    USER_ID
    PROCESS_TYPE
    FUNCTION

    don't know how to make this request.

    Need to shoot where all RRI_ID PROCESSTYPE = 'INFO' and PROCESSTYPE = "RESOLUTION".
    for this RRI_ID.
    This same user, same RRI_ID and 2 processes the records, 1 INFO and RESOLUTION 1
    so unless there is double info, only if there is info and resolution.

    That's what some of the data looks like to...

    PROCESSTYPE | USER_ID. FUNCTION | RRI_ID

    3668 30287 817 INFO
    3748 30287 817 INFO
    4714 30287 817 INFO
    4875 30287 817 INFO
    4882 30287 817 INFO
    4885 30287 817 INFO
    3748 30288 818 INFO
    4714 30288 818 INFO
    4716 30288 818 INFO
    4882 30288 818 INFO
    4885 30288 818 INFO
    819 30289 3668 INFO
    819 30289 4716 INFO
    819 30289 4875 INFO
    819 30289 4882 INFO
    819 30289 4885 INFO
    3668 30290 820 INFO
    4716 30290 820 INFO
    4875 30290 820 INFO
    4882 30290 820 INFO
    4885 30290 820 INFO
    3668 30291 821 INFO
    4714 30291 821 INFO
    4716 30291 821 INFO
    4875 30291 821 INFO
    4882 30291 821 INFO

    If all you need returned is rri_idand rri_id is unique in rrid1, then:

    SELECT r1.rri_id
      FROM rrid1 r1
      JOIN rrid1 r2 ON r1.rri_id = r2.rri_id AND r2.processtype = 'RESOLUTION'
     WHERE r1.processtype = 'INFO'
    

    If rri_id is not unique to rrid1, then the following:

    SELECT UNIQUE r1.rri_id
      FROM rrid1 r1
      JOIN rrid1 r2 ON r1.rri_id = r2.rri_id AND r2.processtype = 'RESOLUTION'
     WHERE r1.processtype = 'INFO'
    

    Published by: Starlight Rider October 1, 2010 11:23
    Corrected my stupid mistake!

  • Help with order by clause

    Hello

    I have a table with the data below, when I run my select with order option gives me
    1 card

    as a first line, but I need it
    12 atm

    as the first line, how this is possible.



    create table test
    (Identification number,
    Val varchar2 (20)
    )


    insert into test values (1, 'Card')
    Insert test values (2, ' map')
    Insert test values (3, ' map')

    Insert test values (4, 'cash')
    Insert test values (5, 'Cash')
    Insert test values (6, 'cash')

    Insert test values (7, "Checque")
    Insert test values (8, "Checque")
    Insert test values (9, "Checque")

    Insert test values (10, 'atm')
    insert into test values (11, 'atm')
    Insert test values (12, 'atm')



    Select * from test
    order by ASC of val

    Or try again with

     select * from test order by upper(val) asc, id desc 
    

Maybe you are looking for