Insert into......table Select b as a from table2 in Mysql - mysql

I migrated from Access to Mysql.Codes for vb6.
mobjCmd.CommandText = "INSERT INTO The123 SELECT The1 AS FirstName FROM schedule"
Set mobjRst = mobjCmd.Execute
And this query is not working in Mysql.

mobjCmd.CommandText = "INSERT INTO The123 VALUES("yOUR VALUES")"
MOBJCMD.COMMANDTEXT = "SELECT The1 AS FirstName FROM schedule"
Set mobjRst = mobjCmd.Execute
You must divide two CMD

You need to specify column for The123 table
mobjCmd.CommandText = #"INSERT INTO The123 (FirstName) SELECT The1 AS FirstName FROM schedule"
Set mobjRst = mobjCmd.Execute

Related

UPDATE in mysql using python

how to do Update in mysql consider Rate is int(8)
k=int(4000);
db=MySQLdb.connect("localhost","user","pass,"databse")
cursor=db.cursor()
sql="UPDATE tablename SET Rate=k WHERE Name='xxx'
cursor=db.cursor()
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
sql should be:
sql = "UPDATE tablename SET Rate = (%s) WHERE Name='xxx'"
and then you should do:
cursor.execute(sql,(k,))
note that k needs to be in a container to be passed correctly to .execute(). in this case it is a tuple.
also int(4000) is redundant. you can just do: k = 4000
if you also want to pass in the name you can do:
sql = "UPDATE tablename SET Rate = (%s) WHERE Name=(%s)"
and then:
cursor.execute(sql,(k,name))

mysql concatenate and increment by one

I am trying to update mysql table MYTABLE using two value. One is STAR column which should be incremented by one on each query, and the second one is COMMENT column which should be concatenated with existing one on each time and separated by comma.
Below is the command I used, but not working.
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, ','.$comment) where ID='$id'";
$query = "update MYTABLE set STAR=STAR+1,COMMENT = CONCAT(COMMENT, ',', '$comment') where ID=$id";
where ID='$id'
is incorrect because $id might be a number, so, delete the "'".
Have you escaped the $comment variable ?
Otherwise you may use prepared statements with PDO :)
I hope you're using PDO...
you should but string in '' and update your query, it has error syntax :
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, '$comment') where ID='$id'";
To make it more secure, just use following code...
$query = "update MYTABLE
set `STAR` = `STAR`+1,
`COMMENT`= CONCAT(COMMENT, '$comment')
where `ID`='$id'";
Happy Coding...

How to insert values from another table

I am inserting two values to a table, one from another table and the other from form control but I am getting an error.
sqlstr = "INSERT INTO partylinks ( accountid, partyid ) values(Select max(accountsinfo.accid) FROM accountsinfo,2 )"
cmd = New OleDb.OleDbCommand(sqlstr, con, tra)
cmd.ExecuteNonQuery()
You don't use the VALUES syntax if you are SELECTing
Try:
sqlstr = "INSERT INTO partylinks (accountid, partyid) Select max(accountsinfo.accid),2 AS partyid FROM accountsinfo"

Changing boolean value in SQL database

I have this query, which spits out that I have an error in syntax. I cannot for the life of me understand what it is. I have a table, where one column is email and the other is subscribed (the latter of which is a boolean using tinyint). Any idea what's wrong with this syntax?
$query = "UPDATE $DB_TABLE SET $DB_IS_SUBSCRIBED_KEY = 0 WHERE $DB_EMAIL_KEY = $email";
Your email value needs to be wrapped in quotes.
UPDATE tablename SET columname = 1 WHERE emailcolumn = "email#email.com"

How to use Boolean logic in a mySQL SELECT query

How can I check if email = '$e' or username = '$e' inside my MySQL query.
Here is my MySQL query so far.
"SELECT user_id FROM users WHERE (email = '$e' AND pass=SHA1('$p'))"
If you want to modify you existing query so that it works even if $e matches username, you can do:
SELECT user_id
FROM users
WHERE (email = '$e' OR username = '$e') AND pass=SHA1('$p')