I want to insert and update a table using one mysql query my table structure is:
student_id ,class_no, section, started_on ,ended_on
I want to update class_no and insert a new records. For that I am using this query:
INSERT INTO student_classes (student_id,class_no,section,started_on,ended_on)
VALUES (835,5,0,2013-04-09,null)
ON DUPLICATE KEY UPDATE class_no = class_no+1
but this query is only insert a new row,not updated column if I use where clause after DUPLICATE KEY UPDATE class_no = class_no+1 its give error message.
Message is:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'where started_on=2013-04-09'
Related
I have query
INSERT INTO subscriptions ( client_id, name, group_id, type )
SELECT clients.id, 'Индивидуал', 0, 1 FROM clients WHERE clients.individual=1;
ALTER TABLE clients DROP COLUMN clients.individual;
ALTER TABLE finance_operations ADD COLUMN sub_id INT NOT NULL DEFAULT 0;
which works fine in Mysql Workbench.
But if I use it in codeIgniter code:
$this->db->simple_query($query);
I get error 1064:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'ALTER TABLE clients DROP COLUMN clients.individual; ALTER TABLE'
at line 4
PDO can not run more than one question one time;
This code is right:
$array = [ "query1", "query2", "query3" ];
foreach($array as $query)
$this->db->simple_query($query);
I would like to show total purchase followed by date from purchase table to insert into total_purchase table as in date and buy as well as view
my purchase table structure as below:
id|companyName |purchase|date
1|housedotcom |1300 |2016-1-1
2|homedotcom |1234 |2016-1-1
3|gardendotco |1000 |2016-1-2
4|landotcom |999 |2016-1-2
5|garagedotcom|5400 |2016-1-2
6|homedotcom |2600 |2016-1-2
7|housedotcom |1900 |2016-1-2
my total_purchase table as below
id|date |buy
1|2016-1-1|2534
2|2016-2-2|20890
I tried this into sql
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
*FROM 'purchase' GROUP BY date;
And it showed in mysql result as I expected but when I tried insert new data into purchase table as same companyName with different date in mysql it says duplicate as well as in php coding sql it did not worked and showed this error
Error Processing RequestSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM purchaseGROUP BY date'
Any suggestions? Thanks in advance.
remove * from your query to be:
INSERT INTO total_purchase (date,buy) SELECT date, sum(purchase)
FROM purchase GROUP BY date;
For inserting value should be entered by just column by column.In Select you use * that return row more than one remove '*'
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
FROM 'purchase' GROUP BY date;
Here is my query
INSERT INTO faq (order, heading, content)
VALUES ('$_POST[order]','$_POST[heading]','$_POST[content]')
I have a field before it called ID that I set to auto increment and INT
I get this error "Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order, heading, content) VALUES ('Order','Title','content')' at line 1"
Is there something I am missing, it works just fine if the ID field doesn't exist?
order is a sql keyword. You need to use backticks to escape the word order
INSERT INTO faq (`order`, `heading`, `content`)
VALUES ('$_POST[order]','$_POST[heading]','$_POST[content]')
Also you should escape those $_POST parameters instead of inserting them directly into your SQL query:
$order = mysql_real_escape_string($_POST['order']);
...
INSERT INTO faq (`order`, `heading`, `content`)
VALUES ('$order','$heading','$content')
I've finally gotten my queries ready to insert into code but now I'm getting an error when running the whole query. I believe it has to do with the drop table function. I originally had them inline and then read that I should remove it and add at the beginning of the query like so:
$query = $this->db->query("DROP TABLE IF EXISTS resultx;");
$query = $this->db->query("DROP TABLE IF EXISTS resulty;");
$query = $this->db->query("
CREATE TEMPORARY TABLE resultx AS
select *, CONCAT(Credit,'_',OrderStat) as consol from (..........
I am creating two temp tables and then joining them in the last query. I am not sure how to put that second DROP temp table back into the full query or if that's even the right way to go.
The error that I'm getting is:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use
near 'CREATE TEMPORARY TABLE resulty AS select packetDeet,Sales,SaleDate, UserID,Lead' at line 15
Query:
CREATE TEMPORARY TABLE resultx AS
select
*,
CONCAT(Credit,'_',OrderStat) as consol
FROM
( select
packetDetailsId, GROUP_CONCAT(Credit) AS Credit,
GROUP_CONCAT(AccountNum) AS AccountNum,
GROUP_CONCAT(OrderStat) AS OrderStat
FROM
( SELECT
pd_extrafields.packetDetailsId,
CASE WHEN
pd_extrafields.ex_title LIKE ('%Credit%')
THEN pd_extrafields.ex_value
ELSE NULL
END as Credit,
CASE WHEN
pd_extrafields.ex_title LIKE ('%Account%')
THEN pd_extrafields.ex_value
ELSE NULL
END as AccountNum,
CASE WHEN
pd_extrafields.ex_title LIKE ('%Existing%')
THEN pd_extrafields.ex_value
ELSE NULL
END as OrderStat
FROM pd_extrafields
) AS myalias
GROUP BY packetDetailsId
)as TempTab;
CREATE TEMPORARY TABLE resulty AS select packetDeet,Sales,SaleDate, .........
Please let me know if this makes sense or I need to update question with more information.
If you are trying to execute both queries in one call to $this->db->query() the problem is probably that your database library does not permit multiple queries.
To see if that is the problem, you should split them up in two separate queries.
I am writing a PHP Code and having problem with conditions of a query. Also if you could tell my how could i accomplish this by using Stored Procedure in Mysql that would be so nice. My task is to run INSERT query if User foreign key don't exist. If User foreign ke is present in the "User_ID" column then update it.But here i can put condition of unique on Column 'User_ID' in database. So no duplicate event will occur from Database.
The following query is showing this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN 23 NOT IN (SELECT User_ID FROM VoiceMail) ELSE UPDATE VoiceMail SET Urg' at line 3
$result=mysql_query("select * from Users",$con);
while($row= mysql_fetch_array($result))
{
$info=getVM($row['Ext'],$ast);
$info[3]=$row['User_ID']; // To insert User_ID from Users table
$query="INSERT INTO VoiceMail (Urgent, New, Old, User_ID)
VALUES ($info[0],$info[1],$info[2],$info[3])
WHEN $info[3] NOT IN (SELECT User_ID FROM VoiceMail)
ELSE UPDATE VoiceMail SET Urgent=$info[0], New=$info[1], Old=$info[2] WHERE User_ID=$info[3]";
if (!mysql_query($query,$con))
{
die('Error: ' . mysql_error());
}
else echo "Database Updated..!";
Is User_Id an unique key? If so, you can use ON DUPLICATED KEY UPDATE
INSERT INTO VoiceMail (Urgent, New, Old, User_ID)
VALUES ($info[0],$info[1],$info[2],$info[3])
ON DUPLICATE KEY UPDATE Urgent=values(Urgent), New=values(New), Old=values(Old)
Just simply query like :
$query="REPLACE INTO VoiceMail (Urgent, New, Old, User_ID)
VALUES ($info[0],$info[1],$info[2],$info[3])";
It will replace values if already exist, insert new if does not exist.
Hopefully it will help