delete query in mysql [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have these tables:
table1
id type name parent group_id
1 special name1 0 21
2 Group name2 1 19
3 Group name3 1 22
4 special name4 0 89
table2
id version mcs user right_id
1 0 15 27 3
2 0 15 27 4
where right_id in table2 references id in table1
what I want is to delete the rows whose name name2 and name3 without using joins
I tried the following:
DELETE FROM table2 WHERE right_id in (19, 22);
DELETE FROM table1 WHERE name in ('name2','name3');
it works but what I want is to put name2 and name3 in variables and not use 19 and 22 directly.

You could use a subquery in the in operator in the first delete:
DELETE FROM table2
WHERE right_id IN (SELECT id
FROM table1
WHERE name IN ('name2', 'name3'))

I think there is a small issue in the question,
DELETE FROM table2 WHERE right_id in (19, 22);
seems to be wrong. If you want to delete ones with the name2 and name3, that relates to the id 2 and 3. As right_id refers to the id, this should be,
DELETE FROM table2 WHERE right_id in (2, 3);
However to answer you, you can use a sub query without joins, like below
DELETE FROM table2 WHERE right_id in (SELECT id FROM table1 where name in ('name2','name3'));

Assuming you're asking to delete from table2 wherever you delete from table1, you want to use nested queries.
DELETE FROM table2 WHERE right_id IN (SELECT id
FROM table1
WHERE name IN ('name2', 'name3'))
EDIT: You could also use join
DELETE FROM table2 as t2, table1 as t1
WHERE t1.id = t2.right_id AND t1.name IN ('name2', 'name3')

Related

How to write a query for this two tables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
EMPLOYEE(id,emp_name)
1, John
2, Michael
3, Philip
===================
RANK(id,emp_id,date,rank)
1,1,1583413078445,SE
2,1,1583413087085,SSE
3,1,1583413092169,PM
4,2,1583413131802,SE
5,3,1583413536655,SE
I want to show
employee id, employee name and latest Rank (whose id is maximum for emp_id in rank table or whose longdate is maximum for emp_id in rank table) from Rank table
id,name,Rank
1, John, PM
2, Michael, SE
3, Philip, SE
I am using java and my database is mysql and sqlite (in case offline)
I tried some way but could not aggregate the queries.
SELECT RK.emp_id, EP.emp_name, RK.rank
FROM RANK RK
LEFT JOIN employee EP ON RK.emp_id=EP.id
GROUP BY RK.emp_id
HAVING MAX(RK.id);
What you need is some techniques on analytic functions.
with RANK_EXPAND as (
select
id,emp_id,date,Rank,
max(date) over (partition by emp_id) as maxDate,
max(id) over (partition by emp_id, date) as maxId,
from RANK
),
select
r.id,
e.emp_name as name,
r.Rank
from
EMPLOYEE e inner join RANK_EXPAND r on e.id=r.emp_id
where
r.date=r.maxDate and r.id=r.maxId
You may also use purely group by + rejoining, but that will make the query much longer.
I agree with the solution of Aditya as the best solution (I would comment the answer, but I don't have necessary reputation).
I just want to point out another possible solution for this using a sub-query in the projection-clause.
SELECT EP.emp_id, EP.emp_name,
(SELECT RK.Rank FROM Rank RK WHERE RK.emp_id=EP.emp_id ORDER BY RK.id DESC LIMIT 1) as Rank
FROM employee EP
;

How randomise the sqlite syntax [duplicate]

This question already has answers here:
Select random row from a sqlite table
(7 answers)
Closed 3 years ago.
I want to randomize some values using ID in sqlite query.
* am using quiz app and want to randomize our question. In database there is a column for category where category id is used to select proper cat and I want randomize only question with limit *
String query = "select * from quiz where id =" +id ;
SELECT *
Says to select all non-hidden columns from the table. The correct syntax for retrieving specific columns is to code the specific column names (or if there are potential ambiguities tablename.columnname e.g. quiz.question).
So you would want
SELECT question FROM quiz .....
To select a random question, assuming that question is the required column name, then you could use :-
String query = "SELECT question FROM quiz ORDER BY random();";
If you wanted to limit the number of rows returned you could use
String query = "SELECT question FROM quiz ORDER BY random() LIMIT 10;";

Joins in Hibernate and its use [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is join in HQL and where shall it be used?
Have gone through tutorial.Majorly three of them used as follows:
Inner Join
Left Outer Join
Right Outer Join
Read few tutorials yet unable to understand.
Join in HQL is same like the SQL joins. In SQL join is used to join two tables to return matching data. In HQL join is between Parent entity and the child entity (Usually on a ForeignKey of a parent primary key).
Person table
person_id | name | dob
Address table
address_id | address_line1 | state | zip | person_id
A SQL Join looks like
Select * from person p join address a on p.person_id = a.person_id and p.person_id = 10
This will return data like following if a person has more than one address
person_id | name | dob | address_id | address_line1 | state | zip
----------------------------------------------------------------------
10 Jack 10/25 22 223 elk blvd AZ 54444
10 Jack 10/25 244 223 NY blvd TX 54344
A HQL join looks like
from Person p join p.address
and it returns Person entity with a list of addresses in it.
Person{
List address = [ Address{ (this is Object) }, Address{(This is object)}]
}

Spring And Hibernate [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to Spring MVC and hibernate, I have two Entity Category and Sub_Category, Sub_category contains Foreign Key(Category_id) how can i insert value in Sub_Category only with the value of Foreign Key.
I my case values are inserting in Sub_category and as well creating new row in Category table and getting the value of that newly creating row id and saving in Sub_Category table but instead i want the value of already existed id value of Category table and store it in sub_Category Foreign Key column.
Let's assume your object is category
after saving it as
category.save(); // after the database insertion
create another object of Sub_Category as
if(category.save()){ // I assume, saved returns true on successful insertion
Sub_Category subCategory = new Sub_Category();
subCategory.setCategory(category); // category that you saved above
subCategory.save();

How do I select the last row of a MySQL table with Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing something for my friend that will involve storing some data in a database with a primary key which is an integer which will increment. For example:
id name Age
1 James 15
2 Max 24
3 Jordan 61
How would I retrieve the integer under the id column in the last row? And this would be with Java. Thanks.
If the "last" row is the one with the highest ID, your SQL (to retrieve just the ID, like you asked) will look like:
SELECT id FROM User ORDER BY id DESC LIMIT 1;
In Java, you'll probably be using JDBC; best practice is to learn how to use PreparedStatements.
is it like
id name age
1 james 15
2 max 24
3 jordan 61
PRIMARY KEY(id)
SELECT id
FROM `tablename`
ORDER BY id DESC
LIMIT 0 , 1
this will return the last row id result set from ur table

Categories

Resources