MySQL Trigger code never called - mysql

I have a MySQL table like this
ID USER_ID FIELD_ID VALUE
1 1 1 0
2 1 2 2
3 1 3 0<----- USER ID 0
. . . .
. . . .
. . . .
40 20 1 6
41 20 2 3
42 20 3 1<------ USER ID 1
. . . .
. . . .
. . . .
73 34 1 9
74 34 2 6
75 34 3 20<----- user ID 20
. . . .
. . . .
. . . .
field_id #3 above contains either "0" or the ID of another user.
I'm trying to write a MYSQL trigger that runs BEFORE UPDATE so that if user "A" is writing the ID of user B into field_id #3, and if user "B" field_id #3 is not "0" then write "0" in to user "A" field_id #3.
so the pseudo code for the trigger is basically:
if (B["field_id3"]!="0") THEN
A["field_id3"]="0";
The MYSQL trigger code is below, but it doesnt do anything, there are no syntax errors, but it doesnt work, anybody can help?
BEGIN
IF (NEW.FIELD_ID='3') THEN
IF (EXISTS(SELECT USER_ID,FIELD_ID,VALUE FROM table WHERE USER_ID=NEW.VALUE)) THEN
IF (FIELD_ID='3' AND VALUE!='0') THEN
SET NEW.VALUE='0'; # <<---NEVER GETS CALLED
END IF;
END IF;
END IF;
END

Related

How to create a conditional calculated column in SQL?

How to write a query to create a conditional and calculated field in SQL?
I am trying to merge two or more entities and the context of the entities are similar to the sample ones below. I am having an issue with regards to how to create the calculated field with the column F and £ of C-A. I would greatly appreciate if I could get some help.
FlightTable AirportTable
AKEY CODE F £ Code City Country
001 LHR C 10 ATL Atlanta USA
002 BOS C 15 BOS Boston USA
003 BOS A 9 . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
101 MAN C 21 VIE Schwechat Austria
102 VIE A 9 ZRH Kloten Switzerland
I am trying to get a result of a joined entity similar to:
Joined table
CODE CITY COUNTRY (£ Calculated Field)
001 BOS Boston USA 4
. . . . . . . . . . . . . .
You may be able to use a case statement in the calculation.
SELECT AIR.Code, AIR.City, AIR.Country, SUM(CASE WHEN flight.f = 'C' then £
when flight.f = 'A' then (-1) * £
else null
end) as "£ calculated"
from AirportTable as "air"
join FlightTable as "flight" on "air".CODE = "flight".code -- could be AKEY = AKEY
I think you just want two joins:
select a.*, (fc.£ - fa.£) as calculation
from airporttable a join
flighttable fc
on fc.code = a.code and fc.f = 'C' join
flighttable fa
on fa.code = a.code and fa.f = 'A';

MYSQL Letters before numbers descending

I am trying to get letters before numbers with a descending order and am finding it very difficult to do so.
I have tried using Case ordering
ORDER BY CASE WHEN ' . $this->sortbycast . ' LIKE \'%[a-z]%\' THEN 0 WHEN ' . $this->sortbycast . ' LIKE \'%[0-9]%\' THEN 1 ELSE ' . $this->sortbycast . ' END DESC
Regular expression as follows
' ORDER BY ' . $this->sortbycast . ' REGEXP \'^[a-z]\', ' . $this->sortbycast . ' desc';
and CAST()
' ORDER BY CAST(' . $this->sortbycast . ' AS UNSIGNED), ' . $this->sortbycast;
But none of these have had the desired result. All work correctly with ascending order. Any help would be appreciated. Thanks.
Update due to Strawberry's response
DB Fiddle here
My present query looks like this - reformatted for legibility:
SELECT p.*
, m.data
, m.id author_id
FROM anchor_posts p
JOIN anchor_post_meta m
ON m.post = p.id
WHERE p.category = 5
AND m.extend = 3
ORDER
BY CAST('anchor_post_meta.data' AS UNSIGNED) -- NOTE: THIS IS A STRING !?!
, m.data DESC
LIMIT 12;
The desired result
The desired result would be to have items show in the following order:
data
==============
A Title
B Title
C Title
D Title
70000
60000
45000
30000
25000
12000
Please see the following version of the DB Fiddle to see more clearly.

Maybe TIMEDIFF function in sql is not accepting column name as one of the parameters

My requirement is to pull records from mysql database which have just 5 mins left from the current time as per the one of the columns in the database. The column is has user inserted datetime.
date_default_timezone_set("UTC");
$utc_time = date("Y-m-d H:i:s", time());
echo "UTC Time: " . $utc_time . "<br>";
$result = mysql_query("select reminder_text, reminder_subject, reminder_date_time_utc $table_name where (TIME_TO_SEC (TIMEDIFF(reminder_date_time_utc , '$utc_time')) < 300) AND (TIMEDIFF(reminder_date_time_utc , '$utc_time')) > 0) ") or die(mysql_error());
here the reminder_date_time inside the TIMEDIFF function is the column name to pick up the DATETIME. Using this query I do not get the results but if I place the date instead of reminder_date_time it gives me the correct output. For example if I say TIMEDIFF('2013-07-12 11:05:00' , '$utc_time') it gives me the correct output. And this same value: 2013-07-12 11:05:00 is actually present in one of the rows of this column reminder_date_time_utc
Any advice where I am going wrong... Does TIMEDIFF function not accept column name as one of the parameters.
Do you forgot the FROM in your sql?
Why dont you try to do it like following:
$utc_time = date("Y-m-d H:i:s", time() - 30000);
and change the query to
'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > "' . $utc_time . '"';
or use DATE_ADD() function :
'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > DATE_ADD(NOW(), INTERVAL -'5' SECOND));

codeigniter - Mysql: Invalid argument supplied for foreach()

function countSv_AV_total(){
$this->db->flush_cache();
$this->db->start_cache();
$sql = "select Verzekeringen.naam,\n"
. " count(*) total,\n"
. " sum(case when (Age <= 18) then 1 else 0 end) Age18,\n"
. " sum(case when (Age BETWEEN 19 AND 30) then 1 else 0 end) Age19,\n"
. " sum(case when (Age BETWEEN 31 AND 50) then 1 else 0 end) Age30,\n"
. " sum(case when (Age BETWEEN 51 AND 69) then 1 else 0 end) Age50,\n"
. " sum(case when (Age >= 70) then 1 else 0 end) Age70\n"
. " from customers\n"
. " INNER JOIN Verzekeringen\n"
. " ON customers.AV=Verzekeringen.verzekeringcode\n"
. " AND Label =1\n"
. " group by Verzekeringen.naam LIMIT 0, 30 ";
$this->db->query($sql);
echo $this->db->last_query().'<br />';
return $this->db->count_all_results('customers','Verzekeringen');
$this->db->flush_cache();
}
I'm using this SQL and in the Mysql workbench it's given the right output. Now I want this to get this to the codeigniter Table class.
I get an error while foreach is not recieving data:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/welcome_message.php
Line Number: 178
Can somebody provide an solution. Thanks in advance

Sorting in mysql

I have a column abc in table t1. The column has values
A1
A2
A3
A4
.
.
A12
B1
B2
.
.
.
B12
C1
C2
.
.
.
C12
H1
.
.
H12
I wanna sort them such that the output is
A1
B1
C1
.
.
H1
A2
B2
C2
.
.
.
H2
.
.
.
.
A12
.
.
.
H12
A select * from abc statement gives A1,A10,A2.... as output. I am trying to use SUBSTR but haven't gotten it right.
This should do it;
SELECT * FROM TEST
ORDER BY SUBSTRING(VALUE, 2) + 0,
SUBSTRING(VALUE, 1, 1);
Demo here.
Considering the format is always a single alpha char followed by any number of digits:
ORDER BY RIGHT(colname, LENGTH(colname)-1)
maybe this:
ORDER BY LEFT(colname,1), RIGHT(colname, LENGTH(colname)-1)
order by lpad(date_created,1,0) ASC
#bfavaretto i think if you use -1 in length colname, and you have 3 characters like in example you will have some strange results.
SELECT Square
FROM Table1
ORDER BY
CASE WHEN Square REGEXP '^[A-Z]{2}'
THEN 1
ELSE 0
END ASC,
CASE WHEN Square REGEXP '^[A-Z]{2}'
THEN LEFT(Square, 2)
ELSE LEFT(Square, 1)
END ASC,
CASE WHEN Square REGEXP '^[A-Z]{2}'
THEN CAST(RIGHT(Square, LENGTH(Square) - 2) AS SIGNED)
ELSE CAST(RIGHT(Square, LENGTH(Square) - 1) AS SIGNED)
END ASC