I have a COLUMN named Col1 in a table.
I want to divide each value in the column Col1by x if the value is less than K and by y if the value is more than K. So I try to update a value by itself and a factor.
I tried something like that but it didn't work :
UPDATE table1 SET Col1 = (SELECT Col1 FROM table1 AS) / IF(val < K, x, y));
I hope you will get me.
Thanks for your help.
(I should be reading a Mysql tutorial, my apologies but I'm not used to handle Mysql and I make query one time a year...)
UPDATE table1 SET Col1 = IF(col1 < K, col1/x, col1/y);
Related
Not sure if what I want to do is possible or not, and if it is I've hit a brick wall in my head of how to do it and searching hasn't found the answer.
I have a Column, we'll call it column A. This is currently blank and I want to populate it with the value from either column B or column C (which is randomly chosen).
In essence what I want to do, but which doesn't work is:
update MyTable
set Column A = Column B OR Column C
From MyTable
Any help would be greatly appreciated!
You can use case. If you want random, then:
update MyTable
set Column_A = (case when rand() < 0.5 then Column_B else Column_C end);
The above randomly assigns either column for each row. If you want all column_As to be either from B or C, but randomly chosen, I think I would use a similar technique:
update MyTable cross join
(select #r := rand()) params
set Column_A = (case when #r < 0.5 then Column_B else Column_C end);
I use Chartio to create dashboards. I'm able to use variables with Chartio that can fill in sections of a MySQL query and then pump out a cool looking graph. I have a situation where I need a query that can have any combination of 3 variables X, Y, Z as shown below.
SELECT orderid
FROM orders
WHERE productcode IN (X) AND
status IN (Y) AND
date IN (Z);
I need to have the ability for the query to "determine" that if I only give it X, ignore Y and Z as a condition, for example. Or if I give it X and Y, ignore Z. I could give it any combinations of those three. By "ignore" I mean not use it as a condition in the WHERE clause.
Is this possible using OR? REGEXP? Wildcards? ...? I'm not very well versed in MySQL. Thanks in advance
if it sets the variable to an empty string when the user leaves the field out, you can write:
SELECT orderid
FROM orders
WHERE (X = '' OR productcode = X) AND
(Y = '' OR status = Y) AND
(Z = '' OR date = Z);
I have two tables x and y I want a column from x to be set to the average of a column from y grouped by a common column.
This what I'v done so far
update
set x.column2 = (SELECT AVG(NULLIF(column2,0))
FROM y group by column1)
on (x.column1 = y.column1)
And I want the value of x.column2 to be updated automatically whenever the value of any row of y.column2 changes.
Note: there is no column have the same name in the two tables.
UPDATE
x
SET
x.column2 = (SELECT AVG(NULLIF(column2,0))
FROM y
WHERE y.column1 = x.column1)
This will run the subquery once per row in x, but the subquery is limited to the rows in y where column1 matches the current x.column1.
For the curious, the internals of this are a bit deeper. In general, all queries (even sub-queries) return table-like objects ("relation" in relational-speak). If the result has only one row, it can be coerced into a 'row' ("tuple" in relational-speak). If the tuple has only one column, it can be further coerced into the value in that column. That is what is going on here. Additionally, no explicit "group by" is needed, because the WHERE clause limits the subquery to only the rows we want to sum, and so we take advantage of the implied 'group all rows' behavior (analogous to adding GROUP BY y.column1)
After your comment, I wanted to show how you would create a "View" for the same thing, which in MySQL means that the aggregated value is not actually 'stored', but calculated in real-time. This means it is never out of date as you insert into y.
CREATE VIEW vx AS SELECT column1, AVG(NULLIF(column2,0) as avg FROM y GROUP BY y.column1
You can then select from vx and in the background it will run that query.
You will need a trigger - see here.
In your case something like
CREATE TRIGGER name AFTER INSERT ON y FOR EACH ROW BEGIN [above statement] END
CREATE TRIGGER name AFTER UPDATE ON y FOR EACH ROW BEGIN [above statement] END
CREATE TRIGGER name AFTER DELETE ON y FOR EACH ROW BEGIN [above statement] END
I did not try out this, so no guarantee for being free of syntax errors (but should not be).
I have two existing columns and have created a new blank column.
Column1 Column2 NewColumn
A B A/B
C D C/D
When I try the following my NewColumn is populated with 1's and 0's. I would like a decimal representation of the proportion.
update MyTable
set NewColumnd = ((Column1/Column2)*1.00)
where Column2 != 0
You're performing the conversion too late - the division has already been performed using integer math, and than the conversion to float occurs. Maybe try:
update MyTable
set NewColumnd = ((Column1*1.00)/Column2)
where Column2 != 0
Although it should be noted that, if the formula should always hold, a computed column would be better than something produced via an UPDATE.
Basically I store data in MySql 5.5. I use qt to connect to mysql. I want to compare two columns, if col1 is greater than col2, the count continues, but when col1 is less than col2, count finishes and exits. So this is to count how many rows under some condition at the beginning of column. Is it possible in mysql?
An example:
Col1 Col2
2 1
2 3
2 1
The count I need should return 1, because the first row meets the condition of Col1 > Col2, but the second row doesn't. Whenever the condition is not meet, counting exits no matter if following rows meet the condition or not.
SELECT COUNT(*)
FROM table
WHERE col1 > col2
It's a little difficult to understand what you're after, but COUNT(*) will return the number of rows matched by your condition, if that's your desire. If it's not, can you maybe be more specific or show example(s) of what you're going for? I will do my best to correct my answer depending on additional details.
You should not be using SQL for this; any answer you get will be chock full of comprimise and if (for example) the result set from your intial query comes back in a different order (due to an index being created or changed), then they will fail.
SQL is designed for "set based" logic - and you really are after procedural logic. If you have to do this, then
1) Use a cursor
2) Use an order by statement
3) Cross fingers
This is a bit ugly, but will do the job. It'll need adjusting depending on any ORDER etc you would like to apply to someTable but the principle is sound.
SELECT COUNT(*)
FROM (
SELECT
#multiplier:=#multiplier*IF(t.`col1`<t.`col2`,0,1) AS counter
FROM `someTable` t, (SELECT #multiplier := 1) v
HAVING counter = 1
) scanQuery
The #multiplier variable will keep multiplying itself by 1. When it encounters a row where col1 < col2 it multiplies by 0. It will then continue multiplying 0 x 1. The outer query then just sums them up.
It's not ideal, but would suffice. This could be expanded to allow you to get those rows before the break by doing the following
SELECT
`someTable`.*
FROM `someTable`
INNER JOIN (
SELECT
`someTable`.`PrimaryKeyField`
#multiplier:=#multiplier*IF(`col1`<`col2`,0,1) AS counter
FROM `someTable` t, (SELECT #multiplier := 1) v
HAVING counter = 1
) t
ON scanQuery.`PrimaryKeyField` = `someTable`.`PrimaryKeyField`
Or possibly simply
SELECT
`someTable`.*
#multiplier:=#multiplier*IF(`col1`<`col2`,0,1) AS counter
FROM `someTable` t, (SELECT #multiplier := 1) v
HAVING counter = 1