I'm trying to make a calculated value to show in another column in another table.
Can someone please explain why this doesn't work
CREATE TABLE #Medition (ID int,AVG decimal(18,4))
INSERT INTO #Medition (ID, AVG)
SELECT ID, SUM(125Hz + 250Hz + 500Hz + 750Hz + 1000Hz + 1500Hz + 2000Hz + 3000Hz + 4000Hz + 6000Hz + 8000Hz)/11 AS AVG FROM tonvarden
UPDATE matningar SET matningar.tonmedelvarde =
#Medition.AVG FROM matningar INNER JOIN #Medition ON matningar.ID =#Medition.ID
DROP TABLE #Medition
I am getting this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO #Medition (ID, AVG) SELECT ID, SUM(125Hz + 250Hz + 500Hz + 750Hz + ' at line 2
No need to create a temporary table to do this.
UPDATE matningar a
join tonvarden b on a.ID = b.ID
set a.tonmedelvarde = (`125Hz` + `250Hz` + `500Hz` + `750Hz` + `1000Hz` +
`1500Hz` + `2000Hz` + `3000Hz` + `4000Hz` + `6000Hz` +
`8000Hz`)/11;
If you would like to update matningar whenever a new row is inserted into tonvarden, then you can create the following trigger:
create trigger update_matningar before insert on tonvarden
for each row
update matningar
set tonmedelvarde =
(new.`125Hz` + new.`250Hz` + new.`500Hz` + new.`750Hz`
+ new.`1000Hz` + new.`1500Hz` + new.`2000Hz`
+ new.`3000Hz` + new.`4000Hz` + new.`6000Hz`
+ new.`8000Hz`)/11
where id = new.id;
Related
My goal is to re-use the result from a SELECT statement to be used in SQL EXISTS statement.
The general idea looks like this:
SELECT *
FROM table
WHERE col1=1
OR EXISTS (
SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col=1
)
The actual SQL statement I am trying to reduce:
"SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} " +
"WHERE user_detail.user=#{username} " +
"OR EXISTS ( " +
" SELECT 1" +
" FROM user_detail " +
" JOIN channel_member ON user_detail.user=channel_member.user " +
" AND channel_member.uuid=#{channelUuid} " +
" WHERE user_detail.user=#{username} " +
")"
You can only do this if your version of MySQL supports window functions, ie. version >= 8.0
You can use conditional window aggregation, like this:
SELECT *
FROM (
SELECT *, COUNT(CASE WHEN col1 = 1 THEN 1 END) OVER () AS CountMatches
FROM table
) t
WHERE CountMatches > 0;
Depending on the number of rows matching to non-matching, this may be more or less performant. You need to test.
This query:
SELECT *
FROM table
WHERE col1 = 1 OR
EXISTS (SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col=1
)
Doesn't really make sense. It is saying to return all rows if col = 1 is in the table -- but then it filters to check if any row has col = 1. So it is equivalent to:
SELECT *
FROM table
WHERE (SELECT 1 FROM table t2 WHERE t2.col = 1);
I strongly suspect that you intend NOT EXISTS -- so get everything with 1. If there is no 1 then return everything:
SELECT *
FROM table
WHERE col1 = 1 OR
NOT EXISTS (SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col = 1
);
This should work fine with tables -- and is in fact probably optimal with the right indexes.
If "table" is really a complex query, then you might consider window functions:
select t.*
from (select t.*,
sum( col = 1 ) as num_1s
from t
) t
where col = 1 or num_1s = 0;
If you want to use any conditional statement on the query you are running you will need to wrap the query and put it in a FROM statement and then run the conditional outside of the query, like so....
SELECT aliasName.*
FROM
(SELECT *
FROM table
WHERE col1=1) aliasName
WHERE EXISTS aliasName //this is the conditionl statement OUTSIDE of the query you built.
Let me know how you do...
For Mr./Ms. Barmar:
The idea
// Idea:
// If one of the row have col1 with value 1.
// Then return all of the row, or return empty []
// SELECT *
// FROM table
// WHERE col1=1
// OR EXISTS (
// SELECT 1
// FROM table
// WHERE col=1
// )
The solution
"WITH temp AS (" +
" SELECT user_detail.user, user_detail.name, channel_member.role " +
" FROM user_detail " +
" JOIN channel_member ON user_detail.user=channel_member.user " +
" AND channel_member.uuid=#{channelUuid} " +
") " +
"SELECT * " +
"FROM temp " +
"WHERE user=#{username} " +
" OR EXISTS ( " +
" SELECT 1" +
" FROM temp " +
" WHERE user=#{username} " +
" )"
The solution above use WITH clause as recommended by Mr./Ms. Barmar, I am posting this, so you can inspect whether this is logical or not.
I have this query:
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
Is there a way to add these values count_1 + count_2 + count_3 + count_4 + count_5 + count_6 and so on.. more efficiently? MySQL keeps crashing for me when I add huge numbers of fields.
Regardless of whether the db design is right or wrong if you use an aggregation function you should use group by
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
GROUP BY name
I have created a multi-page survey form that collects a value of 1 to 10 on each page.
With that data I need to insert 3 different calculations into 3 different columns in the database.
I have created a trigger that adds up all the numbers from the 20 pages and inserts it into the total score column but I need 2 other subscale scores and my version of mysql limits 1 trigger with the same action per table.
is it possible to have one trigger that can insert values into 3 different columns?
I am trying to do this via phpMyAdmin
My deinition below that works for total score:
[![enter image description here][1]][1]
SET NEW.total_score = NEW.answer_01 + NEW.answer_02 + NEW.answer_03 + NEW.answer_04 + NEW.answer_05 + NEW.answer_06 + NEW.answer_07 + NEW.answer_08 + NEW.answer_09 + NEW.answer_10 + NEW.answer_11 + NEW.answer_12 + NEW.answer_13 + NEW.answer_14 + NEW.answer_15 + NEW.answer_16 + NEW.answer_17 + NEW.answer_18 + NEW.answer_19 + NEW.answer_20;
The mysql site - http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
mysql> CREATE TRIGGER ins_transaction BEFORE INSERT ON account
-> FOR EACH ROW PRECEDES ins_sum
-> SET
-> #deposits = #deposits + IF(NEW.amount>0,NEW.amount,0),
-> #withdrawals = #withdrawals + IF(NEW.amount<0,-NEW.amount,0);
comma separating the columns seems to be the solution
So for your code it would be like...
SET NEW.total_score = NEW.answer_01 + ... + NEW.answer_20,
NEW.other_column = NEW.answer_01 + NEW.answer_02;
I'm having a serious brain fart over this but essentially i have a table that looks similar to this -
+ id + staff_id + location + date + dismiss_boolean +
+------+-------------+-------------------+------------+-----------------+
+ 1 + 22 + Bedfordshire + 2011-11-01 + 0 +
+ 2 + 22 + Hertfordshire + 2011-11-02 + 1 +
+ 3 + 16 + Bedfordshire + 2011-12-01 + 0 +
+ 4 + 17 + Bedfordshire + 2011-11-22 + 0 +
+ 5 + 77 + Hertfordshire + 2011-11-01 + 1 +
+ 6 + 77 + Cambridgeshire + 2011-11-01 + 1 +
What i'm after is (in a single query) -
If the row exists, ie: there is a row where staff_id = 6 and location = Bedfordshire, then UPDATE the row only if the date field is older than X date.
Otherwise, if the row doesn't exist (there isn't a row where staff_id = 6 and location = Bedfordshire) then INSERT the data as a new row.
Usually you would use -
INSERT....ON DUPLICATE KEY UPDATE...
But you can't, iirc, use WHERE clauses in the UPDATE statement if using ON DUPLICATE. And again you can't use UNIQUE Indexes on the location and staff_id fields due to duplicates.
So i'm after a query along the lines of -
IF(
(SELECT COUNT(*) FROM `notifications` WHERE `staff_id` = '6' AND `location` = 'Bedfordshire') > 0
, UPDATE `notifications` SET `dismiss_boolean` = '1', `date` = '2011-12-10' WHERE `staff_id` = '6' AND `location` = 'Bedfordshire' AND `date` < '2011-12-10'
, INSERT INTO `notifications` (`staff_id`, `location`, `date`, `dismiss_boolean`) VALUES ('6', 'Bedfordshire', '2011-12-10', '1')
)
But that throws syntax errors and it's incorrect use of the IF function from what I remember.
So has anyone got any ideas how I can accomplish this? The only solution i can think of is to query the table prior to updating or insert the data but as said, ideally i want to do this in a single query.
Any help will be appreciated as I've been rattling around this problem for most of the day and I've yet to come up trumps searching Google/Stackoverflow.
I am not sure if this is still actual, but just for reference:
INSERT INTO notifications
(staff_id, location, `date`, dismiss_boolean
VALUES ('6', 'Bedfordshire', '2011-12-10', '1')
ON DUPLICATE KEY UPDATE
dismiss_boolean = if(VALUES(`date`) < '2011-12-10',
VALUES(dismiss_boolean),
dismiss_boolean),
`date` = if(VALUES(`date`) < '2011-12-10',
VALUES(`date`),
`date`);
I wanted to combine 11 columns of a form into single column.
here is my query
SELECT DormData.BuildingID,
BuildingDetails.BuildingName,
Contractors.Item,
ActionDetails.ActionType,
(DormData.Note1
+ DormData.Note2
+ DormData.Note3
+ DormData.Note4
+ DormData.Note5
+ DormData.Note6
+ DormData.Note7
+ DormData.Note8
+ DormData.Note9
+ DormData.Note10
+ DormData.Note11) AS Notes
FROM ActionDetails
INNER JOIN (Contractors
INNER JOIN (BuildingDetails
INNER JOIN DormData
ON BuildingDetails.BuildingID = DormData.BuildingID)
ON Contractors.ID = DormData.ItemID)
ON ActionDetails.ActionID = DormData.ActionID;
But not getting required result.
You need to replace the + with a &