insert in mysql if select statement is true - mysql

I have a MySQL query :
select count(id) as tot from user where sponsor_id=10006 and exp > now();
The above query returns 1 as tot.
If the above query returns more than 1, it should insert a record in a table.

You can write something like:
insert into log
select 'there are more than one IDs'
from dual -- this is just a dummy table in mysql
where 1 <> (select count(id) as tot
from user
where sponsor_id=10006
and exp > now()
)
You can check this demo on SQLFiddle (without the exp>now() part).

Related

Generate random numbers in MySQL without repeat

I have a stored procedure that insert data related to a scooter rent, one of the fields is a "rent code" when i insert the information "rent code" should be a unique random number that don exist in the table "rents"
here is that i tried
SELECT FLOOR(RAND() *9999)+1 AS random_num
FROM reservaciones
WHERE "random_num" NOT IN (SELECT reservaciones.Codigo_Reservacion FROM reservaciones)
LIMIT 1
in the table i got
1
2
3
when i run the code if i reduce the limit to 4 (example) the query still generates the numbers that i already got in my table
Create and use user-defined function similar to:
CREATE FUNCTION generate_random_number (upper_limit INT)
RETURNS INT
BEGIN
IF upper_limit = (SELECT COUNT(*) FROM main_table WHERE id <= upper_limit) THEN
RETURN NULL;
END IF;
IF upper_limit IS NULL THEN
SELECT REPEAT('9', LENGTH(MAX(id) + 1)) FROM main_table INTO upper_limit;
END IF;
RETURN (SELECT t1.id + 1
FROM (SELECT id FROM main_table
UNION ALL
SELECT 0) t1
LEFT JOIN main_table t2 ON t1.id = t2.id - 1
WHERE t2.id IS NULL
AND t1.id < upper_limit
ORDER BY RAND() LIMIT 1);
END
upper_limit parameter specifies the range from which the number should be generated. Can be less than max. existing value. Cannot be greater than 2147483647. Cannot be set to NULL if max. existing value is 999999999 or greater (you may expand this limit by set the parameter and output datatypes to BIGINT).
Function returns NULL if there is no free number in specified range.
fiddle with some comments.

Mysql insert with IF condition is it possible?

I have a mysql table with only 3 columns (id - primary key / status / timestamp). Periodically I receive a status message which can be 0 or 1 and I need to insert it in that table but only if the last status inserted is different.
Is there any way to do this is only one mysql query ?
Basically I need to do this :
SELECT status FROM `status_e_distr_c01` order by id DESC LIMIT 1
Then compare my status message with select result and if is different then do insert, if not...do nothing...
So how can I do this in one mysql query ?
Thank you very much
Assuming that the values of the columns that you want to insert are :status, :timestamp you can do it like this:
INSERT INTO status_e_distr_c01 (status, timestamp)
SELECT :status, :timestamp
FROM dual
WHERE :status <>
COALESCE((SELECT status FROM status_e_distr_c01 ORDER BY id DESC LIMIT 1), NOT :status)
The code will work even if the table is empty.
You may omit FROM dual if your version of MySql is 5.7+.
See a simplified demo.
INSERT INTO status_e_distr_c01 (status)
SELECT #new_status
FROM ( SELECT status
FROM status_e_distr_c01
ORDER BY ts DESC LIMIT 1 ) subquery
WHERE subquery.status != #new_status;
PS. Will fail if destination table is empty. The most first row must be inserted without thes checking.
UPDATE
INSERT INTO status_e_distr_c01 (status)
SELECT #new_status
WHERE #new_status != COALESCE(( SELECT status
FROM status_e_distr_c01
ORDER BY ts DESC LIMIT 1 ), 2);
Works on empty table too.
fiddle
The same as stored procedure:
CREATE PROCEDURE insert_if_changed (IN new_status INT)
INSERT INTO status_e_distr_c01 (status)
SELECT new_status
WHERE new_status != COALESCE(( SELECT status
FROM status_e_distr_c01
ORDER BY ts DESC LIMIT 1 ), 2);
fiddle
You can use an INSERT ... SELECT ... FROM the dual pseudo table and check if the target table is empty or the new status differs from the last one selected in a subquery.
INSERT INTO status_e_distr_c01
(status)
SELECT ?
FROM dual
WHERE NOT EXISTS (SELECT *
FROM status_e_distr_c01)
OR ? <> (SELECT status
FROM status_e_distr_c01
ORDER BY id DESC
LIMIT 1);

How to insert data with subquery using SELECT * as VALUE?

I have 2 queries.
First query is
SELECT date
FROM tableA
WHERE
db='mydb' AND
date > (SELECT MAX(date) FROM tableB WHERE db='mydb')
LIMIT 1;
Second query is
INSERT INTO tableB (date, db, comment)
SELECT * FROM tableA
WHERE
db = 'mydb' AND
date = (date that i got from first query);
Data types are:
db(VARCHAR), date(date), comment(mediumblob).
statement SELECT * FROM tableA WHERE db = 'mydb' AND date = (date that i got from first query); returns exactly 1 row, but somehow second query returns that 1242 - Subquery returns more than 1 row.
I have tried to change SELECT * statement to each columns, but it didn't work on column comment. And it is impossible to do this on trigger because tableA is in system table.
As far as I understand the goal, you are trying to copy all fresh values from A to B. Here "fresh" means "date not exists in the target table yet".
I think it can be simplified to this query:
INSERT INTO `tableB` (`date`, `db`, `comment`)
SELECT `date`, `db`, `comment`
FROM `tableA`
WHERE `db` = 'mydb'
AND `date` > (SELECT MAX(`date`) FROM `tableB` WHERE `db` = 'mydb')

CASE Statement instead of inner join

This is my table structure:
CUST_ID ORDER_MONTH
---------------------
1 1
1 5
2 3
2 4
My objective is to tag these customers as either New or Returning customers.
When I filter the query lets say for month 1 then customer 1 should have the tag 'New' but when I filter it for month 5 then customer 1 should show up as 'Return' as he already made a purchase in month 1.
Same way customer ID 2 should show up as New for month 3 and return for month 4.
I want to do this using a CASE statement and not inner join.
Thanks
If you insist on using a case statement, the logic would be something like "If this is the first month for that user, write new, otherwise write returning." The query would be as follows:
SELECT CASE
WHEN m.month = (SELECT MIN(month) FROM myTable WHERE customer = m.customer) THEN 'New'
ELSE 'Returning' END AS customerType
FROM myTable m;
However, I think this would be nicer and more readable in a JOIN. You can write an aggregation query to get the earliest month for each user, and then use COALESCE() to replace null values with 'Returning'. The aggregation:
SELECT customer, MIN(month) AS minMonth, 'New' AS customerType
FROM myTable
GROUP BY customer ;
To get the rest:
SELECT m.customer, m.month, COALESCE(t.customerType, 'Returning') AS customerType
FROM myTable m
LEFT JOIN(
SELECT customer, MIN(month) AS minMonth, 'New' AS customerType
FROM myTable
GROUP BY customer) t ON t.customer = m.customer AND t.minMonth = m.month;
Here is an SQL Fiddle example that shows both examples.
You don't need a JOIN and a case statement would probably be overkill...
SELECT CUST_ID, IF(COUNT(1)>1, 'Returning', 'New') AS blah
FROM the_table
WHERE ORDER_MONTH <= the_month
GROUP BY CUST_ID
;
Of course, using just month is going to cause problems after a year (or really, after passing December.)
This would be better
SELECT CUST_ID, IF(COUNT(1)>1, 'Returning', 'New') AS blah
FROM the_table
WHERE order_date <= some_date
GROUP BY CUST_ID
;
Well I do not reccomend this way but this is what you want.
select *
,case when order_month = (select MIN(order_month) from #temp t2 where t1.cust_ID =t2.cust_id) THEN 'NEW' ELSE 'Return' end 'Type'
from #temp t1
I think I get what you're trying to do. Your case statement basically just needs to check if the customer's month equals the month you're filtering by. Something like this:
SELECT
<your other fields>,
CASE WHEN Order_Month = <your filter> THEN 'New'
ELSE 'Return'
END AS 'SomeName'
FROM <your table>
Try this query
select a.CUST_ID, a.ORDER_MONTH ,case when b is not null then 'Return' else 'New' end as type
from tablename a
join tablename b on a.CUST_ID=b.CUST_ID and a.ORDER_MONTH>b.ORDER_MONTH
SELECT *,
CASE
WHEN EXISTS (SELECT *
FROM [YourTable] t2
WHERE t1.cust_id = t2.cust_id
AND t2.order_month < t1.order_month) THEN 'Return'
ELSE 'New'
END
FROM [YourTable] t1
This query uses CASE on an EXISTS clause.
The EXISTS is on a subquery which queries the same table for any rows in previous months.
If there are rows for previous months then the EXISTS is true and the CASE returns 'Return'. If there are no rows for previous months then the EXISTS is false and the CASE returns 'New'.

Catching latest column value change in SQL

How can I get the date for the latest value change in one column with one SQL query?
Possible database situation:
Date State
2012-11-25 state one
2012-11-26 state one
2012-11-27 state two
2012-11-28 state two
2012-11-29 state one
2012-11-30 state one
So result should return 2012-11-29 as latest change state. If I group by State value, I will get the date for first time I have that state in database.
The query will group the table on state and show the state and in the date field the latest date created of that state.
From the given input the output would be
Date State
2012-11-30 state one
2012-11-28 state two
This will get you the last state:
-- Query 1
SELECT state
FROM tableX
ORDER BY date DESC
LIMIT 1 ;
Encapsulating the above, we can use it to get the date just before the last change:
-- Query 2
SELECT t.date
FROM tableX AS t
JOIN
( SELECT state
FROM tableX
ORDER BY date DESC
LIMIT 1
) AS last
ON last.state <> t.state
ORDER BY t.date DESC
LIMIT 1 ;
And then use that to find the date (or the whole row) where the last change occurred:
-- Query 3
SELECT a.date -- can also be used: a.*
FROM tableX AS a
JOIN
( SELECT t.date
FROM tableX AS t
JOIN
( SELECT state
FROM tableX
ORDER BY date DESC
LIMIT 1
) AS last
ON last.state <> t.state
ORDER BY t.date DESC
LIMIT 1
) AS b
ON a.date > b.date
ORDER BY a.date
LIMIT 1 ;
Tested in SQL-Fiddle
And a solution that uses MySQL variables:
-- Query 4
SELECT date
FROM
( SELECT t.date
, #r := (#s <> state) AS result
, #s := state AS prev_state
FROM tableX AS t
CROSS JOIN
( SELECT #r := 0, #s := ''
) AS dummy
ORDER BY t.date ASC
) AS tmp
WHERE result = 1
ORDER BY date DESC
LIMIT 1 ;
I believe this is the answer:
SELECT
DISTINCT State AS State, `Date`
FROM
Table_1 t1
WHERE t1.`Date`=(SELECT MAX(`Date`) FROM Table_1 WHERE State=t1.State)
...and the test:
http://sqlfiddle.com/#!2/8b0d8/5
If you add another column 'changed datetime' you can fill this using an update trigger that inserts NOW(). If you query your table ordering on the changed column, it will endup first.
CREATE TRIGGER `trigger` BEFORE UPDATE ON `table`
FOR EACH ROW
BEGIN
SET ROW.changed = NOW();
END$$
Try this ::
Select
MAX(`Date`), state from mytable
group by state
If you had been using postgres, you could compare different rows in the same table using "LEAD .. OVER" I have not managed to find the same functionallity in mysql.
A bit hairy, but I think this will do:
select min(t1.date) from table_1 t1 where
(select count(distinct state) from table_1 where table_1.date>=t1.date)=1
Basically, this asks for the first time no changes in state is found for any later values. Be warned, it may be this query scales terribly for large data sets....
I think your best choice here are analytical functions. Try this - it should be OK performance-wise:
SELECT *
FROM test
WHERE my_date = (SELECT MAX (my_date)
FROM (SELECT MY_DATE
FROM ( SELECT MY_DATE,
STATE,
LAG (state) OVER (ORDER BY MY_DATE)
lag_val
FROM test
ORDER BY MY_DATE) a
WHERE state != lag_val))
In the inner select, the LAG function gets the previous value in the STATE column and in the outer select I mark the date of a change - those with lag value different than the current state value. And outside, I'm getting the latest date from those dates of a change... I hope that this is what you needed.
SELECT MAX(DATE) FROM YOUR_TABLE
Above answer doesn't seem to satisfy what OP needs.
UPDATED ANSWER WITH AFTER INSERT/UPDATE TRIGGER
DELCARE #latestState varchar;
DELCARE #latestDate date;
CREATE TRIGGER latestInsertTrigger AFTER INSERT ON myTable
FOR EACH ROW
BEGIN
IF OLD.DATE <> NEW.date THEN
SET #latestState = NEW.state
SET #latestDate = NEW.date
END IF
END
;
CREATE TRIGGER latestUpdateTrigger AFTER UPDATE ON myTable
FOR EACH ROW
BEGIN
IF OLD.DATE = NEW.date AND OLD.STATE <> NEW.STATE THEN
SET #latestState = NEW.state
SET #latestDate = NEW.date
END IF
END
;
You may use the following query to get the latest record added/updated:
SELECT DATE, STATE FROM myTable
WHERE STATE = #latestState
OR DATE = #latestDate
ORDER BY DATE DESC
;
Results:
DATE STATE
November, 30 2012 00:00:00+0000 state one
November, 28 2012 00:00:00+0000 state two
November, 27 2012 00:00:00+0000 state two
The above query results needs to be limitted to 2, 3 or n based on what you need.
Frankly it seems like you want to get max from both columns based on the data sample you have given. Assuming that your state only increases with the date. Only I wish if the state was an integer :D
Then union of two max sub queries on both columns would have solved it easily. Still a string manipulation regex can find what's the max in state column. Finally this approach needs limit x. However it still has lope hole. Anyway it took me sometime to figure your need out :$