473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

IF statement in MySQL?

Atli
5,058 Expert 4TB
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 30745
You have to understand the difference between SELECT and INSERT statement.
Mar 5 '07 #2
ronverdonk
4,258 Expert 4TB
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 Expert 4TB
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 Expert 4TB
That is what I am here for!

Ronald :cool:
Mar 5 '07 #5
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....could 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
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
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...
5
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...
2
by: - | last post by:
is there an equivalent of postgresql's PERFORM statement in mysql?
0
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,...
2
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 (...
1
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...
4
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
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...
1
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 '<'. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.