简体   繁体   中英

Sql statement error basic

Still getting to grips with sql but this seams like a pretty basic problem im obviously missing something big

SELECT * FROM myTable WHERE arbitraryInt < (SELECT MAX(arbitraryInt));

I get no rows returned when i know for a fact that there are arbitraryInt values that are less than the max. Theres no error though just nothing returned. Thanks in advance

update: this is the rest of the code which still gets an error with FROM keyword

UPDATE myTable SET arbitraryInt = arbitraryInt + 1 
WHERE primaryKey = 0001 AND arbitraryInt < (
    SELECT MAX(arbitraryInt) FROM myTable
);

I get the error myTable is soecified twice

The context is im trying to make items manually sortable using a sorting integer for each item. So the reason for MAX is to keep that integer from getting bigger than necessary. I'm sure theres a bette way to do this but i havent seen it. The result im looking for is a table with up and down arrows to re order list items for an online store.

A subquery must be a complete query, capable of being run on its own. Your subquery

(SELECT MAX(arbitraryInt))

is not complete, as it does not specify a FROM clause. Your complete query should probably be something like

SELECT *
  FROM myTable
  WHERE arbitraryInt < (SELECT MAX(arbitraryInt)
                          FROM myTable);

You are missing a from clause, so presumably you intend:

SELECT *
FROM myTable
WHERE arbitraryInt < (SELECT MAX(arbitraryInt) FROM mytable);

This has feature in stack overflow before. Have a look at MYSQL update with WHERE SELECT subquery error

The following code seems to be something like what you are asking for
/*
CREATE TABLE MYTABLE (PRIMARYKEY INT, ARBITRARYID INT, AB1 INt)
*/

truncate table MYTABLE;
INSERT INTO MYTABLE
VALUES (1,1,NULL),(1,2,NULL),(1,3,NULL);

update  MYTABLE 
set     ARBITRARYID = ARBITRARYID + 1
where   PRIMARYKEY = 1 and
        ARBITRARYID <=  (
        SELECT MAXABID FROM
        (
        select max(ARBITRARYID) MAXABID FROM MYTABLE
        ) S
        )
;
select * from MYTABLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM