I have a Account-Database with a "last-login" row, I want to delete all accounts which are empty (NULL) at last-login row.
Does someone know a MySQL Query for this? (Database-Structure is like: User-Id, password, last-login)
This should do the the tricks:
DELETE FROM Account-Database WHERE last-login IS NULL;
or if the field is just an empty field (not null but '')
DELETE FROM Account-Database WHERE last-login= '';
See: http://dev.mysql.com/doc/refman/5.0/en/delete.html
delete from Account-Database where `last-login` is NULL;
It's not clear in your question whether Account-Database is the table name or the database name but you get the idea.
<?php
mysql_query("Delete from Account-Database where last-login = NULL");
?>
DELETE FROM `Account-Database` WHERE `last-login` IS NULL;
The syntax for a DELETE query is as follows:
DELETE FROM `tablename` WHERE conditions
Related
I have ONE column in MySQl table which contains this format:
https://open.spotify.com/track/AAABBBCCC
and I'd like to leave just AAABBBCCC, and not the entire column! The last parte is always the same.
Is that possible? Thanks and sorry for my english!
You can use SQL REPLACE function to do that.
UPDATE your_table_name
SET your_column_name = REPLACE(your_column_name, 'https://open.spotify.com/track/', '')
For more information you can read SQL statement to remove part of a string
Use REPLACE to delete the wanted text from your column:
UPDATE Table1 SET Column1 = REPLACE(Column1,'https://open.spotify.com/track/','')
I have a simple database query that I can not figure out for some reason.
DELETE * FROM Wishlist WHERE (id, uid) VALUES ("18","34i274o1y24ouy1o4");
This might be just wrong syntax in general. My skills are pretty low when it comes to databases. Any ideas? Just trying to delete a row.
Thanks in advance!
It looks like you were trying to use the INSERT syntax to do a deletion. If you want to remove records with the criteria you gave try this:
DELETE
FROM Wishlist
WHERE id = '18' AND uid = '34i274o1y24ouy1o4'
You should use IN:
DELETE FROM Wishlist WHERE (id, uid) IN ('18','34i274o1y24ouy1o4');
Or just use AND:
DELETE FROM Wishlist WHERE id = '18' AND uid = '34i274o1y24ouy1o4';
DELETE FROM Wishlist WHERE id = '18' AND uid = '34i274o1y24ouy1o4';
In addition to other answers using IN keyword,
DELETE FROM Wishlist where id='18' and uid = '34i274o1y24ouy1o4' also works.
NOTE that you do not need asterisk in DELETE operation.
If id is the primary key, where id = '18'without uid should be enough.
Also, you might want to consider making id an auto increment column with type INT. It is faster to query and you can get rid of the quotes like where id = 18.
my problem is that I want to delete one record on my table "provicional" if "Id" and "Password" match... i have a simple code but just work for ID and I want to delete it if both match!
$sql = "DELETE FROM `provicional` WHERE `id` IN ('3','basico')";
many thanks for any help
A not so popular way of doing this in MySQL would be:
DELETE FROM provicional WHERE (id, password) = (3, 'basico')
Note you can add more than just 2 elements in each list.
Tip: Do not use apostrophes ' around numeric fields.
$sql = "DELETE FROM `provicional` WHERE `id` IN ('3') AND `password` IN ('basico')";
I have a large mySQL database and I want to remove every record that is empty, not null in a certain column. What is the best way to do write a SQL query for this?
Currently I have tried:
DELETE FROM Businesses WHERE WEBADDRESS IS NULL
But it did not delete anything. There are 44,000 records and almost 80% of them are null in that column.
DELETE FROM myTable WHERE myColumn IS NULL
Link to MySQL page for DELETE syntax: http://dev.mysql.com/doc/refman/5.7/en/delete.html
IF the column is not NULL but just blank you would need to do something like:
DELETE FROM myTable WHERE myColumn = ''
Based on the information you also provided in the comments, the values are likely being loaded as empty ('') and not NULL: http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html
The second query should work.
delete from your_table
where certain_column is null
DELETE from `<tablename>`
WHERE `<columnname>` is null
delete from table_name where column=''
delete from table_name where column_name is null;
this query will definitely work for you.
Don't worry about count of data. Mysql can handle it. Just write:
DELETE FROM `Businesses` WHERE `WEBADDRESS` = '';
I'm wondering if it's possible to move all data from one column in table to another table.
Here's what i'm trying to do:
Table 1 : users - columns trying to read+move = oauth_access_key and oauth_access_secret
Table 2 : account_users - target columns: oauth_token_key, oauth_token_secret
The relation key between these tables is "user_id".
Is this possible in one query? I know this is easily done in PHP, but i'm wondering if this can be done in plain SQL.
Thanks in advance.
UPDATE users, account_users
SET account_users.oauth_token_key=users.oauth_access_key,
account_users.oauth_token_secret = users.oauth_access_secret
WHERE account_users.user_id=users.user_id;
You can use JOIN syntax on MySQL Update.
I think the answer you are looking for is
INSERT INTO `account_users` (`user_id`, `oauth_token_key`, `oauth_token_secret`)
SELECT `user_id`, `oauth_access_key`, `oauth_access_secret` FROM `user`
ON DUPLICATE KEY UPDATE
`oauth_token_key` = VALUES(`oauth_token_key`),
`oauth_token_secret` = VALUES(`oauth_token_secret`);
EDIT
I am assuming you want to move data as in 'put it somewhere it hasn't been yet'.
EDIT2
Here is a documentation on VALUES(): http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_values
Since the title is SQL, and not DB specific... here's a solution for those who stumble upon this while searching for Postgres:
UPDATE account_users
SET oauth_token_key = users.oauth_access_key,
oauth_token_secret = users.oauth_access_secret
FROM profiles
WHERE account_users.user_id = users.user_id;
INSERT INTO account_users (user_id, oauth_token_secret)
SELECT users.user_id, users.oauth_access_secret FROM users
ON DUPLICATE KEY UPDATE account_users.oauth_token_secret = users.oauth_access_secret