mysql if exists return the record value instead of regular output

I want a select that if/exists it returns the ‘link’ value instead of regular output. so instead of ‘1’ it returns the ‘link’ Is that possible? SELECT IF( EXISTS( SELECT link FROM modules WHERE module=’license’ ), 1, ‘/dashboard/’ ) >Solution : Use aggregation with MAX() (or MIN()): SELECT COALESCE(MAX(link), ‘/dashboard/’) link FROM modules WHERE module… Read More mysql if exists return the record value instead of regular output

retrieving first rehire and terminated dates and second rehire and termination of employee

I want to write a SQL Query to fetch 1st rehire, termination date and 2nd rehire, termination dates of employee if employee doesn’t having those dates then it will display blank(null) in result. Having table name as per_periods and date start column is contains hire/rehire date and actual termination date contains terminations dates.In image 1st&2nd… Read More retrieving first rehire and terminated dates and second rehire and termination of employee

How to get last 3 days records from database without today?

I am using this code: WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) And result will be like this: 2022-05-12 2022-05-11 2022-05-10 But I want this: 2022-05-11 2022-05-10 2022-05-09 >Solution : Use a range here: WHERE date < CURDATE() AND date >= DATE_SUB(CURDATE(), INTERVAL 3 DAY) Assuming today’s date be 2022-05-12, the above logic would exclude… Read More How to get last 3 days records from database without today?

MySQL stored prod insert from IN data and other table

I have an INSERT statement below, taking from 3 inputs that will be passed from a php post request. The statement below returns an error INSERT INTO orderitems (orderId, productId, quantity, productName, price) (korderid, kproductid,kquantity, SELECT p.productName, p.price from products p WHERE p.id=kproductid) The error being: The following query has failed: "CREATE PROCEDURE `CreateOrderItem`(IN `korderid`… Read More MySQL stored prod insert from IN data and other table

How to determine next insert id value (inside a transaction) before actually inserting a new record?

If I have a table with an id column (Autoincrement). How to get the value of the next insert id before actually inserting a new record in the same transaction? >Solution : You can do this query: SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_SCHEMA, TABLE_NAME) = (‘mydatabase’, ‘mytable’); (Of course you would name your database and… Read More How to determine next insert id value (inside a transaction) before actually inserting a new record?