I want to update a table column where the field is blank with a zip code from within the column as the Name field is same for the 2 rows.
so the shipping zip blanks need to be filled with the same shipping zip where name is the same.
Thanks
The attached is the table screenshot
You didn't provide an actual table name. I will use table_name as a way to show you what to do. I am using the table_name twice, so each time I give it an alias (m & f). So that we can do compares
Slow answer
UPDATE table_name m
SET m.shipping_zip =
(SELECT f.shipping_zip
FROM table_name f
WHERE f.Name=m.Name and f.shipping_zip<>'')
WHERE m.shipping_zip = '';
Fast answer (but untested)
UPDATE table_name m, table_name f
SET m.shipping_zip = f.shipping_zip
WHERE m.Name=f.Name AND m.shipping_zip='' AND f.shipping_zip<>'';
This can be done like this
update `table` t, (select * from `table`
where shipping_zip is not null and shipping_zip != '')q
set t.shipping_zip = q.shipping_zip
where t.Name = q.Name;
Related
In this query:
SELECT *
FROM general_settings AS general_settings
JOIN settings_attribute AS settings_attribute ON settings_attribute.id = general_settings.settings_attribute_id
JOIN user_settings AS user_settings ON general_settings.user_settings_id = user_settings.id
WHERE
(settings_attribute.name = 'AAA' AND brand_settings.AAA <> general_settings.value)
OR
(settings_attribute.name = 'BBB' AND brand_settings.BBB <> general_settings.value)
OR
(settings_attribute.name = 'CCC' AND brand_settings.CCC <> general_settings.value)
;
I want a way using MySQL or Redshift to use settings_attribute.name as column name to avoid writing all the attribute names as a static in the query,
for example like this:
SELECT *
FROM general_settings AS general_settings
JOIN settings_attribute AS settings_attribute ON settings_attribute.id = general_settings.settings_attribute_id
JOIN user_settings AS user_settings ON general_settings.user_settings_id = user_settings.id
WHERE
brand_settings.#settings_attribute.name <> general_settings.value
;
No, this is not possible. In SQL, all identifiers (e.g. column names) must be explicit and fixed in the query at the time it is parsed, so the SQL engine can verify that the columns actually exist before it begins executing. It's not possible for a query to name different columns based on the string values it reads during execution.
What would happen if your settings_attribute.name contained 'XYZ' in some row, but there was no column by that name? It would be an error if you named a column that didn't exist, but in SQL that is checked at the time the query is parsed.
hi I have a list of primary keys of other tables in my field k in table A like this :
id=1 and ref=2 and fun=1
id=1 and ref=5 and fun=2
id=2 and ref=1 and fun=1
and I have a table B which it's primary key is id,ref,fun
now I want to select all records from table B where primary key matches the values in table A
,
of course the
select * from B where A.k
.. works
but get one record by one select
I ask for select all records from table B that matches A.k in table A.
thank you in advance.
MySQL will not treat the value of A.k as an expression to re-evaluate -- it's not substituted into the SQL and executed recursively. When you write WHERE A.k, it simply tests whether the contents of the k column is true or false.
You shouldn't put all the values in a single column. You should have separate columns for id, ref, and fun in table A. Then you can join the tables:
SELECT B.*
FROM B
JOIN A ON B.id = A.id AND B.ref = A.ref AND B.fun = A.fun
If you really want to have SQL expressions in a table column, you'll need to create dynamic SQL in a stored procedure.
SET #where = (SELECT GROUP_CONCAT(CONCAT('(', k, ')') SEPARATOR ' OR ')
FROM A);
PREPARE stmt FROM CONCAT('SELECT * FROM B WHERE ', #where);
EXECUTE stmt;
See the MySQL documentation on Prepared Statements
I have: something like
UPDATE table
SET field = (SELECT field FROM another_table WHERE id = #id);
Problem: SELECT field FROM another_table WHERE id = #id subquery can return one field or EMPTY SET.
Question: How to handle situation when subquery returns empty set?
Updated:
UPDATE table t
SET field = IF((SELECT field FROM another_table WHERE id = #id) IS NOT NULL, -- select field
(SELECT field FROM another_table WHERE id = #id), -- Problem #1: select field AGAIN!
(SELECT field FROM table WHERE id = t.id) -- Problem #2: try to not change value, so select the current field value!!
);
If function can be useful:
UPDATE table
SET field = if((SELECT field FROM another_table WHERE id = #id) IS NULL,true,false);
You can add the conditional:
WHERE (SELECT COUNT(*) FROM another_table WHERE id = #id) > 0
This will make sure that at least one row exists in another_table with the id. See my SQL Fiddle as an example.
Note: this may not be the most efficient because it does a count on another_table, and if it is greater than 1 it will do another SELECT (two sub-queries). Instead, you can do an INNER JOIN:
UPDATE table
INNER JOIN another_table ON table.id=another_table.id
SET table.field = another_table.field
WHERE another_table.id = #id;
See this SQL Fiddle. The reason why I saved this as a second option, is not all SQL languages can UPDATE with joins (MySQL can). Also, you need some way to relate the tables..in this case I said that the table.id we are updating is equal to another_table.id we are taking the data from.
NOTE The UPDATE statement will modify EVERY row in table and assign the same value to every row; that seems a little unusual.
To answer your question:
If you want to handle the "empty set" by not updating any rows in table, then one way to do this is with a JOIN to an inline view:
UPDATE table t
CROSS
JOIN (SELECT a.field
FROM another_table a
WHERE a.id = #id
LIMIT 1
) s
SET t.field = s.field
Note that if the inline view query (aliased as s) return an "empty set", then no rows in table will be updated, because the JOIN operation will also return an "empty set", meaning there are zero rows to be updated.
As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:
tbFiles: pkFileID, fileType, fileName
tblFileType: pkFileType, typeName, typeDesc
I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:
UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType
but I always seem to get 0 row(s) affected.
When I run
SELECT DISTINCT fileType
FROM `tblFiles`
I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)
I know this must be simple, but why is the UPDATE query not affecting 23 rows?
You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:
UPDATE tblFileType t1
INNER JOIN
(
SELECT DISTINCT fileType
FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType
Update: Since the table tblFileType is blank, you will need to use INSERT something like:
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType
FROM tblFiles
WHERE -- a condition here
you just want to populate the table - not update anything in there (especially since nothing exists yet)
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles
I have a pure theoretical question, with a nonsense example:
UPDATE mytable
binaryData = '___GIANT_BINARY_DATA___',
isBig = LENGTH('___THE_SAME_GIANT_BINARY_DATA___') > 1000000000
WHERE id = 22
Now, if my binary data is a "gillion bytes", i want to avoid to write it twice in the plain SQL
UPDATE mytable
binaryData = '___GIANT_BINARY_DATA___',
isBig = LENGTH(binaryData) > 1000000000
WHERE id = 22
I want to update a column field, then re-use it, using its column name, in the same query
or maybe is there a way to define an alias in the UPDATE syntax, like i can do with SELECT?
thank you in advance
(p.s. i'm also interested in to the equivalent INSERT syntax)
You can use a CROSS JOIN like so:
UPDATE mytable a
CROSS JOIN (SELECT '__GIANT_BINARY_DATA__' AS bindata) b
SET a.binaryDate = b.bindata,
a.isBig = LENGTH(b.bindata) > 1000000000
WHERE a.id = 22
Which will give you access to that same value in every row, and you only have to pass in the data once in the SQL statement string.
MySql is an oddity in that the SET statements are non-atomic, meaning that as soon as one column is assigned a new value, that new value will be reflected if it is used elsewhere in the update statement.
The following statements:
CREATE TABLE Swap (
a CHAR(1),
b CHAR(1)
);
INSERT INTO Swap (a, b) VALUES ('a', 'b');
UPDATE Swap SET a = b, b = a;
SELECT * FROM Swap;
Will result in b, b in MySql, but b, a in every other RBDMS that I'm aware of...
So for your question, you don't need to alias binaryData, because soon as it is updated, the updated value will be reflected in the isBig assignment statement.
However, it's probably a bad idea to rely on this behavior, since it is non-standard.
You can use a user variable:
set #content = '___GIANT_BINARY_DATA___';
UPDATE mytable
SET binaryData = #content,
isBig = LENGTH(#content) > 1000000000
WHERE id = 22;
set #content = NULL; -- free up memory