SQL query: Show all from A that have B as C - mysql

Id Parinte Angajator
1 Parinte1 Firma1
2 Parinte2 Firma2
3 Parinte3 Firma3
Id Copil Data_Nastere Id_Parinte Data_creare
1 Copil1 10.01.2013 1
2 Copil2 11.11.2012 1
3 Copil3 10.10.2013 2
4 Copil4 12.11.2013 2
I have these 2 tables (1st let's say table1 and 2nd table2)
I need to do the following operations on these 2 tables.
I need them in a query so I can copy paste it for project!
Show all from "Parinti" that have "Angajator" as "Firma1"
Show all from "Copil" that have "Parinte1"
Update the field "Data_creare" from table2 with current date for "Copil" that have "Parent1"
Delete all from "Copil" that have "Data_Naster" = 10.01.2013
last I need to sort the values from table2 column "Copil" ascending ascending depending of field "Data_nastere"

At least you have to type it yourself!

Related

SQL update where select from the same table [duplicate]

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

How can I copy rows from one to another table with a different colnm data

I had two tables Table 1 & Table 2 AS shown here
Table:1
ID
IMG_PATH
CAT_ID
166
hfhbf
1
164
jgj
2
162
ggd
1
160
mfnf
1
158
dbd
2
Table:2
ID
IMG_PARENT_ID
Here I want to print table 1's ID column data Example:166
Here (ID-1) Example:165
Here I want to print table 1's ID column data Example:164
Here (ID-1) Example:163
Here I want to print table 1's ID column data Example:162
Here (ID-1) Example:161
Here I want to print table 1's ID column data Example:160
Here (ID-1) Example:159
Here I want to print table 1's ID column data Example:158
Here (ID-1) Example:157
AS SHOWN IN TABLE 2 I NEED FOLLOWING VALUE...
and dont try this manually method:
INSERT INTO tabla2
SELECT * FROM tabla1
WHERE id = 1 //Here we write the condition
I want to fetch data because arround 10,000's row are inserted in this table
Lots of tries but didnt get it
based on what you provided info about your question, this is what I understand about this.
Assuming that table 1 is auto_increment with ID of 1-10,000.
Then you can use this to select the even IDs in table 1 and insert it to table 2
insert into table2 (ID) select ID from table1 group by ID having mod(ID, 2) = 0;
To select odd IDs from table 1 and insert it to table 2 you can use this
insert into table2 (IMG_PARENT_ID) select ID from table1 group by ID having mod(ID, 2) = 1;

SQL select single row with two matching values

I'm probably having a bad day, but this is somehow escaping me:
I want to return the second row in this table only.
userId val1 val2
1 11 12
2 13 14
3 13 15
4 16 17
Using SELECT * FROM table WHERE val1=13 AND val2=14 obviously returns 2 rows, the second and third. Whats the correct way to select ONLY the second row? Where val1 is 13 and val2 is 14?
EDIT: I'm an idiot.
Just use SELECT * FROM table WHERE val1=13 AND val2=14like you already mentioned in your question, because in fact, it actually returns only row number 2.
If it has been a very bad day & there is a typo in your question & val2 in third row also equals 14 - the only way your query would return two rows, then this would do what you want
SELECT *
FROM table
WHERE val1=13 AND val2=14
ORDER BY userId
LIMIT 1;
If you get 2 rows, then there must be 2 rows match the condition.
Maybe you could try:
select count(*)
from table
where val1=13 AND val2=14;
to show the size of result set.

SQL group by, how to define which record for each group, eg. the latest, is used

I have a table that stores a reference for each product-identifier, however there are some duplicate records - ie. a product may have been submitted more than once so has more than one reference. Each record is timestamped with the updated column.
I need a query that will only give one (non-empty) reference per product-identifier but that crucially will only select the LATEST record for each product.
So if the original table is this:
id updated product-identifier reference
------------------------------------------------------------
1 2014-11-10 07:47:02 9876543210123 98043hjdww98324322
2 2014-11-10 07:53:24 9897434243242 89f7e9wew329f080re
3 2014-11-12 10:51:10 9876543210123 48308402jfjewkfwek
4 2014-11-12 12:53:24 9876543210123 89739432bkjfekwjfk
5 2014-11-12 12:55:16 9876543210321 21321hhfioefhewfoe
6 2014-11-13 01:01:10 9897434243242
7 2014-11-13 01:05:24 9897434243242 1232423jhdksffewfe
The query should return just these records:
id updated product-identifier reference
------------------------------------------------------------
4 2014-11-12 12:53:24 9876543210123 89739432bkjfekwjfk
5 2014-11-12 12:55:16 9876543210321 21321hhfioefhewfoe
7 2014-11-13 01:05:24 9897434243242 1232423jhdksffewfe
I have tried
SELECT * FROM tablename WHERE reference !='' GROUP BY product-identifier ORDER BY updated DESC
and this gives only one record for each product, but not the latest - it is grouping before sorting.
Help greatly appreciated!
I usually do this by having a subquery that selects the highest timestamp for each group (product_identfier in your case) and then use that to select the row I want. Like this
select *
from tablename a
where a.updated = (select max(updated)
from tablename b
where a.product_identifier = b.product_identifier)
There are many ways to do this. If you want the latest record, here is a method using not exists:
select t.*
from tablename t
where not exists (select 1
from tablename t2
where t2.product_identifier = t.product_identifier and
t2.updated > t.updated
);

Performing calculations when inserting in MySQL

The scenario im facing is given below:
Table 1:
ID Name Age1
1 X 22
2 Y 23
Table 2:
ID Name Age2
1 XX 45
2 YY 55
I need to add a new column in Table 1 which is equal to
(TABLE1.AGE1/TABLE2.AGE2) * 100
Can I perform this using MySQL?
Thank you in advance for your help
First Introduce the column, then add the data. Something like
ALTER TABLE Table1 ADD Ratio double NULL AFTER Age1;
UPDATE Table1, Table2
SET Table1.Ratio = (Table1.Age1/Table2.Age2) * 100
WHERE Table1.ID = Table2.ID
You can't do it with a single query.