473,549 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IF statement in MySQL?

Atli
5,058 Recognized Expert Expert
Hi.

I'm trying to create a query that would check a couple of fields, and depending on the results, insert a row into the table.

This is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. SELECT IF(
  2.   (SELECT count(IP) FROM tbl_CounterLog WHERE Date = NOW() AND IP = '172.0.0.1') > 0,
  3.   2,
  4.   (INSERT INTO tbl_Test(IP, Date) VALUES('172.0.0.1', NOW()))
  5. ) AS 'Result'
  6.  
This keeps giving me an error at the 'INTO' in the insert statement, but if I replace it with a select statement if works fine.
Perhaps it's not possible to use INSERT as a sub-query.

Anybody know of a way to do this?
Mar 4 '07 #1
5 30747
write2ashokkumar
39 New Member
You have to understand the difference between SELECT and INSERT statement.
Mar 5 '07 #2
ronverdonk
4,258 Recognized Expert Specialist
Apart from the SELECT, which you have to issue separately, why not use
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tbl_Test(IP, Date)
  2.  SELECT tbl_CounterLog.IP, tbl_CounterLog.Date
  3.   FROM tbl_CounterLog WHERE tbl_CounterLog.IP='172.0.0.1' AND tbl_CounterLog.Date=NOW();
Ronald :cool:
Mar 5 '07 #3
Atli
5,058 Recognized Expert Expert
Thanks for the suggestion Ronald.

The two tables I used in my query tbl_Test and CounterLog are supposed to be the same table, my mistake. Tho it doesn't really matter in our queries .

The way you wrote it it duplicates the values in the table if the values already exists.
What I was trying to do is the exact opposite to that, that is write the values if they don't already exist.

However, thanks to you, I did manage to do that.
The Date field is a NOT NULL field, so I rewrote your query so it either inserts the row or triggers a NULL error.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tbl_Test(IP, Date)
  2. SELECT '172.0.0.1',  IF(
  3.      (SELECT count(IP) FROM tbl_Test WHERE IP = '172.0.0.1' AND Date = NOW()) = 0,
  4.      (SELECT NOW()),
  5.       NULL
  6.     );
  7.  
So thanks again Ronald for pointing me in the right direction.
Mar 5 '07 #4
ronverdonk
4,258 Recognized Expert Specialist
That is what I am here for!

Ronald :cool:
Mar 5 '07 #5
imran haq
2 New Member
That is what I am here for!

Ronald :cool:
That is what I am here for!

Ronald :cool:

Hi Ronald;

How are you ?

I have 3 rather Long Questions that are causing alot of trouble:
I would appreciate your help since i have had a really hard time, and tried to use ur last post to atli to help but it did not help...

!) I have an events table, within the events table I have 7 columns,
Alias varchar (30)
Progress int(1)
Val double
Stamp timestamp
ID auto increment.
date date
time time

My problem is very similar to Atli, where he wanted to perform a mysql if statement on the result of a count function.
Basically, My progress column is intented to hold a '1' or a '0'

so here we go....

if the result of the count of occurance Alias = 'Test' modulus 2 (i.e. count(Alias) mod 2)
is equivelant to '0', then we wish to input a '1' into the 'progress' column of my 'events' table, if the result of count(Alias) mod 2 where Alias = 'Test' equals '1' then i would like to put in a '0' into my 'progress' column of my 'events' table.

This process must happen every time that a record is entered with the Alias = 'Test' - so that probably means that we need to put it in a trigger of some sort.

Also is there a way for replacing Alias = 'Test' for Alias = 'select distinct alias from events'????? so that we have something like this:

Select count(Alias) mod 2 from events where Alias = Select distinct Alias from events. and then enclose that in an if statement and then a trigger.

This 2nd query is ideally what we would like because my table could potentially hold 1000 records or even more.

thankyou

2) in the same 'events' table i have a stamp column, holding a timestamp that has been inserted into my table, what i need to do is, to get what ever is in the stamp column, and cut it into time and date, the way i thought would be good was to insert that value into the date and time columns that are in my events table????
and again this would have to be for every row in my table, and every newly inserted row, and would therefore require a trigger....coul d you help me out at all?
this has also given me a huge hadache.

one Last Question: will the trigger come into action every time the MYSQL server instance is initialised, you see everything is local at the moment on my laptop, and is configured so that the instance starts upon boot, will that mean that the triggers will also come into play asap?



Thankyou very much for your help in advance - this is very urgent and i have been bouncing my head of walls for days.
I really appreciate your time and effore
imran
Jan 31 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
3159
by: Andie | last post by:
Hello All, I have this sql statement thats works in MSSQL when I call a function function showNewProd(dispNum) 'Declare some variables dim mySQL, rsTemp, tempStr, count 'Read Database
4
3259
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant section from the manual: 20.1.9.7 WHILE Statement
5
1614
by: Fred | last post by:
Hi all, I have done a lot of experimentations using several "or" predicates within an sql select statements on mysql. My only conclusion so far is that whatever syntax or order I use it is always slower than doing seperate sql statements (one for each or) and programaticaly adding them to a vector.
2
1651
by: - | last post by:
is there an equivalent of postgresql's PERFORM statement in mysql?
0
1701
by: phil | last post by:
I am having a problem with the C api with prepared statements, the data recorded in the database does not match the data I am sending. It seems to be some sort of bit shifted version of the data, and I have no idea why. I am including code I have been using to test this, followed by the output in the database. I am using mysql version...
2
13913
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks ( weekstart datetime not null primary key, weekend datetime not null )
1
1484
by: luvic.vangool | last post by:
Hi, I'm fairly new to MySql and come from a MS SQL background. I have an application that I am converting from MS SQL to MySql. I need to return a value from MySql using a parameter. In MS SQL my select statement is: "select @id = id from database"
4
13059
by: Roy Epperson | last post by:
I'm not finding what the maximum number of characters that a statement can be. Anyone know where I can find that spec?
3
7690
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running the My Java Application.; Therefore I used this following codes, It Consists the seperate parts for the Raede and Member Class for the purposely I...
1
2502
by: Maklar60 | last post by:
I am attempting to execute an INSERT statement on my page but continually get the following error: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Incorrect syntax near '<'. /int_code04/myNMLC/insertNewTrackRecord.asp, line 97 I've tested the INSERT stmt both within SQL Server and as a string literal within the page's code...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.