I want to update a whole table sav where the column phone contains the id from a table stock_phone. I want to set the sav.phonecolumn to the stock_phone.imei value, here is the query I tried:
UPDATE sav JOIN
stock_phone
ON sav.phone = stock_phone.id
SET sav.phone = stock_phone.imei;
But then the sav.phone value is set to 2147483647 for every row, and this value doesn't match with any imei value from stock_phone.
I search about how to UPDATE and JOIN but my syntax seems correct according to the questions I read.
Finally solved it myself..
I'm trying to set a too big integer into an INT(8) field. The 2147483647 value means Incorrect Integer, that's why the value isn't matching with one of my records.
Hope it will help some people.
2147483647 equals to 2^31-1 limit of INT(4). IMEI number (15 digits) are greater of this value.
I am curious if this returns the same results:
UPDATE sav s
SET s.phone = (SELECT sp.imei FROM stock_phone sp WHERE s.phone = sp.id);
I don't see a problem with updating the key used for the JOIN, but perhaps that is triggering some sort of bug.
Related
I am a beginner in MySQL and i want to do a check for values but within a range, i have this:
SELECT t1.width, COUNT( t1.width )
FROM test t1
INNER JOIN (
SELECT t2.width
FROM test t2
GROUP BY width
HAVING COUNT( t2.width ) >1
)t2 ON t1.width BETWEEN (t2.width +1000) AND (t2.width -1000)
ORDER BY t1.width
So what i want to do is to check if there is two values of 'width' with a difference of +1000 or -1000.
The result is always null.
could you please tell me what is wrong with the query?
I don't fully understand what your data is. The way I understand is you are looking to see if two values from two columns have a specific difference, i.e the first value in the first column is 2000 and the first value in the second column is 1000, since there is a difference of 1000 you want this noted. You could use the CASE function (more detail here https://www.w3schools.com/sql/func_mysql_case.asp).
Say you have one column called width_1 which consists of different values of widths, and a second column called width_2 which also consists of different width values, all contain in a table called width_table, you could use the following:
SELECT
CASE
WHEN width_1 - width_2 = 1000 OR width_1 - width_2 = -1000 THEN TRUE
ELSE FALSE
END AS column_name
FROM width_table ;
This will produce a column whose entries are either 1 if the difference is exactly +1000 or -1000, or 0 if the difference is anything else.
If you want to check if the difference is between 1000 and -1000, then you can use the following:
SELECT
CASE
WHEN width_1 - width_2 BETWEEN -1000 AND 1000 THEN TRUE
ELSE FALSE
END AS column_name
FROM width_table ;
I am trying to fill in fields in a table with date of another table.
In the table 'blanko' I have a column 'product_sku' and 'virtuemart_product_id'.
In the table 'jml_virtuemart_products' I have (among others) the columns 'product_sku' and 'virtuemart_product_id'.
Now I want to add values from jml_virtuemart_products.virtuemart_product_id column into the the same column in 'blanko' from rows with where product_sku is the same.
I am trying with this query and it works partialy.
UPDATE blanko b1 SET virtuemart_product_id = (SELECT virtuemart_product_id FROM jml_virtuemart_products v1 WHEREe v1.product_sku = b1.product_sku);
The problem is that it add endless amount of rows with NULL values.
Can someone explain what I am doing wrong? I am running in circles...
Better way is to use join to update the record
update blanko b1
join jml_virtuemart_products v1 on v1.product_sku = b1.product_sku
set b1.virtuemart_product_id = v1.virtuemart_product_id
I have a Customer table, my client want to not physically delete any record from this table so I use a TINYINT field "IsDeleted" to keep track of deleted customers.
Now i m in a situation where i need to exclude Deleted Customers but when i tired following Query it gives me less number of records
select count(*) from customer where IsDeleted <> 1; (Count = 1477)
then the following
select count(*) from customer where (IsDeleted = 0 or IsDeleted is null); (Count = 1552)
why the above query counts are different?
why "NULL" value is not counted in " IsDeleted <> 1" check?
Please suggest.
Like Duniyadnd and triclosan point out this is caused by the column type for IsDeleted.
Change the query in the right panel so you can see the difference between using int and varchar column types. sqlfiddle.com/#!2/7bf0a/5
You cannot correct use comparing operations for NULL-values. Consider to change type of IsDeleted to enum('N','Y') with Not NULL option.
Null is not an int. As soon as you did <> 1, that means it would only look at ints. Null ideally means something that does not exist (that's why a lot of people use it instead of 0 in case you do store 0s in the table).
If you only have null, 0 and 1 values in the isDeleted column, you would probably find a difference between the two queries (1522-1477) to be the total number of nulls in your table (75).
I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)
For the following table (all columns are integers)
[id, value, best_value]
For a given id and value I want update it's row setting the best_value column to max(newvalue,best_value). I seached into the documentation but I dont see a function for doing so.
Thanks
You want GREATEST(x,y). Example, if the new value is 530:
UPDATE my_table SET best_value = GREATEST(530,best_value) WHERE id=123
You don't strictly need any such function,
UPDATE my_table SET best_value = new_value
WHERE id=123 AND best_value < new_value
would do the job about as well as AlienWebguy's answer :)