I am trying to delete the duplicates that I am getting in my database using DELETE function of my SQL with LIMIT 1 but it is showing me the "LIMIT" syntax error .
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users (name VARCHAR , age INT(3))");
myDatabase.execSQL("INSERT INTO users (name, age) VALUES ('Vaishant', 21)");
myDatabase.execSQL("INSERT INTO users (name, age) VALUES ('Tommy',4)");
myDatabase.execSQL("DELETE FROM users WHERE name = 'Vaishant' LIMIT 1");
Can someone tell me why I am getting this error and how to correct it ?
SQLite does not support LIMIT in a DELETE statement.
Use a subquery that returns the rowid of a row that contains the name that you search for:
String sql = "DELETE FROM users WHERE rowid = (SELECT rowid FROM users WHERE name = 'Vaishant' LIMIT 1)";
myDatabase.execSQL(sql);
If you want to delete the duplicate names and keep only 1, then you can do this:
DELETE FROM users
WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.name = users.name AND u.rowid < users.rowid)
or:
DELETE FROM users
WHERE rowid NOT IN (SELECT MIN(rowid) FROM users GROUP BY name)
In your delete statement you are using limit with Delete query. You should use it as below:
First add id column as primary key in your table
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users (ID INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR , age INT(3))");
change the query of delete as below
myDatabase.execSQL("DELETE FROM users
WHERE id IN
(SELECT id FROM
(SELECT id,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) AS row_num FROM users )t
WHERE row_num > 1)");
Related
So i have two tables: locations and employees i want locations_id to be the same in employees.locations_id, I am trying to make it all in one statement
the erros is this 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 'INSERT INTO employees(employees_id, locations_id) VALUES('e1598','')' at line 1
String sql = " INSERT INTO locations( locations_id , username, password, id, type_of_id, first_name, last_name, phone, email, date_of_birth, address, sex ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)"
**Error here --->** + "INSERT INTO employees(employees_id,locations_id) VALUES (?,SELECT locations_id FROM locations INNER JOIN employees ON locations.locations_id =employees.locations_id)";
try {
MicroModelGUI micro = new MicroModelGUI();
PreparedStatement consulta = micro.connection.prepareStatement(sql);
consulta.setString(1, tflid.getText());
consulta.setString(2, tfuser.getText());
consulta.setString(3, tfpass.getText());
consulta.setString(4, tfid.getText());
consulta.setString(5, tftoid.getText());
consulta.setString(6, tffirst.getText());
consulta.setString(7,tflast.getText());
consulta.setString(8,tfphone.getText());
consulta.setString(9,tfemail.getText());
consulta.setString(10,tffdn.getText());
consulta.setString(11,tfaddress.getText());
consulta.setString(12,tfsex.getText());
consulta.setString(13,tfeid.getText());
int resultado = consulta.executeUpdate();
You should be using an INSERT INTO ... SELECT here:
INSERT INTO employees (employees_id, locations_id)
SELECT ?, l.locations_id
FROM locations l
INNER JOIN employees e ON l.locations_id = e.locations_id;
To the ? placeholder you would bind a value from your Java code. Note that while your version of SQL might support putting a scalar subquery into a VALUES clause, it is likely that your exact version would cause an error, as it would return more than one row.
I am trying to insert a value in a middle table, which is created by having two other tables relationship many to many. So this middle table has 2 foreign keys(idProduct and idWarehouse), one for each table and an additional row(storage). So I have trouble inserting in this third row.
It gives this error:
Result: near "SELECT": syntax error
At line 1:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage)
VALUES
(SELECT
This is my code:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage)
VALUES
(SELECT id FROM Products
WHERE nameProduct = 'cheese',
SELECT id FROM Warehouse
WHERE nameWarehouse = 'Vegas',
'10')
Each of the queries must be enclosed inside parantheses:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage) VALUES
(
(SELECT id FROM Products WHERE nameProduct = 'cheese'),
(SELECT id FROM Warehouse WHERE nameWarehouse = 'Vegas'),
'10'
);
This will work if theses queries return always only 1 row, so both columns nameProduct and nameWarehouse must be unique.
The statement can also be written without the VALUES clause, with SELECT:
INSERT INTO ProductsInWarehouse (idProduct, idWarehouse, storage)
SELECT
(SELECT id FROM Products WHERE nameProduct = 'cheese'),
(SELECT id FROM Warehouse WHERE nameWarehouse = 'Vegas'),
'10';
I am created a prepared select query and it appears the query is not picking up the DESC or I have the bind_param structured wrong. I am trying to get the last id of the user_id's image to display. The user's image displays, but it is the first id image they have. I tried doing ASC and it was the same thing.
Am I doing this right?
$sql = "
SELECT *
FROM profile_img
WHERE user_id = ?
ORDER BY ? DESC LIMIT 1
";
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param("ss", $user_id, `id`);
$stmt->execute();
if (!$stmt->errno) {
// Handle error here
}
$stmt->bind_result($id, $user_id, $profilePic);
$pics = array();
while ($stmt->fetch()) {
$pics[] = $profilePic;
}
echo '<img id="home-profile-pic" src=" '.$profilePic.'">';
}
I don't think you can :
Use placeholders in an order by clause
Bind column names : you can only bind values -- or variables, and
have their value injected in the prepared statement.
You can use number instead of field name in the 'order by' clause
Why you have put ? after "order by" statement?
Your order by should reference to either id of your "profile_img" table or any timestamp field in that table...
e.g. $sql = "
SELECT *
FROM profile_img
WHERE user_id = ?
ORDER BY id DESC LIMIT 1
";
here replace id (i am assuming this name) with the primary key field name of profile_image table
or
$sql = "
SELECT *
FROM profile_img
WHERE user_id = ?
ORDER BY created_on DESC LIMIT 1
";
here created_on (which i have also assumed) can be replaced by any timestamp field if you any in profile_img table
I need to delete the a specific student from stdnt table and insert that students data into a new table stdnt_log including the 'leaving_date' field, which would be the record's deletion date
sql = "INSERT INTO stdnt_log SELECT rollno, name, grade, leaving_date FROM stdnt WHERE rollno = ?";
sql = "DELETE FROM stdnt WHERE rollno = ?";
Simpler option - if you can/want to apply it - is to alter the stdnt table and add another column: deactivation_date:
alter table stdnt add deactivation_date date;
Once someone gets deactivated, just update that column:
update stdnt set
deactivation_date = sysdate
where student_id = some_value;
Active students would then be
select * from stdnt where deactivation_date is null;
Simpler to implement, easier to maintain. Drawback? Table will be larger and larger, but hey, this is Oracle, it handles zillions of rows without problems.
I have two tables:
TABLE1 has columns(id,name,password),
TABLE2 has columns(id,salary,dept).
id in table1 is auto-generated sequence number. Data is added using java form. I wanted to show table1 id in table2 along with other details.
create or replace PROCEDURE PRO5(y in varchar,z in varchar,x in varchar,
c in varchar,d in varchar,b in number, j in number)
as BEGIN
insert into emp_general(username,email,password) values(y,z,x);
insert into SALARY_DET(username ,salary,company,dept) values(y,c,d,b);
set j := INSERT INTO salary_det(ID) SELECT ID FROM emp_general;
END;
Try this,
select #Id = convert(int,scope_identity());
For Oracle
SET LID = LAST_INSERT_ID();