How do I add two sql queries and result the final value of the second column ?
Example?
TABLE ONE
colum 1 | colum 2
10 | 5
RESULT:
TABLE ONE
colum 1 | colum 2
0 | 15
Query:
select colum 1 + colum 2 from TABLE ONE
Are you looking to update the rows with the squashed values?
UPDATE Table1 t1 SET t1.column2 = t1.column1 + t1.colum2, t1.column1 = 0;
Related
I got the following structure:
admin_id || country_id
---------------------------------
1 2
5 1
1 2
2 3
5 62
1 1
3 62
How to fetch all values by taking the $_SESSION['admin']['id'], finding the country_id of that admin and getting all other admin_id and country_id that are the same of the session admin?
So, lets say the currently logged in admin has id = 5 , that means the admin_id: 5 has two country_id: 1 and 62. I want to take all rows that have country_id: 1 and 62.
It should return this:
admin_id || country_id
------------------------
5 1
5 62
1 1
3 62
How can I do this in one sql query?
You can use a where clause for filtering on the admin_id or the country_id:
select t.*
from t
where t.admin_id = 5 or
t.country_id in (select t2.country_id from t t2 where t2.admin_id = 5);
SELECT t1.*
FROM table t1
JOIN table t2 USING (country_id)
WHERE t2.admin_id = 5
fiddle
I've a table syntax like this,
- table1
id foreign_table_id
-----------------------
1 2
2 2
3 2
4 1
5 1
6 1
The other table is,
- table2
id value
------------
1 20
2 10
I want to get a summation of table1.foreign_table_id where the data will retrieve from table2.
Like this example should produce the result 90
Please provide any solution.
Use below query.
Select sum(table2.value) as total FROM table1 LEFT JOIN table2 ON table1.foreign_table_id = table2.id
It will give u below result.
Output::
total
-----
90
SELECT SUM(table2.value) AS summation FROM table1 LEFT JOIN table2
ON table2.id = table1.foreign_table_id
Let's say I have a table with these columns and values:
id field
-----------
1 0001
2 0002
3 0003
4 0004
5 0005
Then i have this other table with these fields and values:
id tid info_1 field_1 info_2 field_2
---------------------------------------------------
1 1 7 0000 null null
2 2 null null 10 0002
3 3 17 0003 null null
4 4 29 1111 null null
5 6 null null 44 0005
I need an update query that will update the records of table 2, just in the info columns, but only if table 1 has matching fields in table 2 and also only when id of first table and tid of second table match.
So for example table 1 id of 1 and table 2 tid of 1 match but field_1 or field_2 doesn't have the same field number as table 1.
Table 1 id of 2 and table 2 tid of 2 match and field_2 does have a matching number as the field in table one, so i would like to change info_2 of record 2 of table 2 to a number of my choosing. let's just say 50. in all cases the number in info_1 or info_2 will be changed to 50.
Now table 1 id of 3 and table 2 tid of 3 match and field_1 has matching number as the field in table 1, so i need to change info_1 to 50 for that 3rd record in table 2.
In record 4 the id and tid match but no fields match so skip that one.
In record 5 even though field_2 of table 2 matches field in table one, the id of first table and tid of second table do not match, so that 5th record can be skipped.
Here is what i tried so far:
$updatequery1 = "UPDATE table2 LEFT JOIN table1 a ON a.field = field_1 SET info_1 = 50";
$updateresult1 = mysql_query($updatequery1) or die(mysql_error());
So for this first attempt i didn't set id and tid to match, i just went right for looking for field matching. i also didn't incorporate field_2. not even sure if it can all be done in one pass or if multiple queries are needed. in any case, this attempt made all info_1 values equal to 50 even if there were no field matches.
Can someone help me with this query and make both info columns change at same time if possible?
If i were to do a select query for selecting all records that match in table 1 id and table 2 tid, it would be like this, but don't know how to incorporate it into the update query.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
I can also try and incorporate field matching and it would probably be something like this:
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
WHERE table1.field = table2.field_1
OR table1.field = table2.field_2
But again, wouldn't know how to turn that into an update query so that either info_1 or info_2 changes accordingly.
So to summarize in a quicker way. if id and tid match and field and field_1 match, info_1 should change to 50. if id and tid match and field and field_2 match, info_2 should change to 50
you can use CASE statement in the SET
UPDATE Table2 as T2
JOIN Table1 T1
ON T2.tid = T1.id
set
info_1 = CASE WHEN T2.field_1 = T1.field then 50 ELSE T2.field_1 END ,
info_2 = CASE WHEN T2.field_2 = T1.field then 50 ELSE T2.field_2 END
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
Is there a way I could somehow do the following?
Table ONE
id c_id type
-------------------
1 1 7
2 2 7
3 3 5
4 4 7
Table TWO
id title live
-------------------
1 row1 1
2 row2 0
3 row3 0
4 row4 1
The c_id column links the data from table ONE to table TWO. Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2".
I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1.
Here's how I thought I'd do it, but this doesn't seem to work:
SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5
I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1.
You were close... try using an implicit join:
SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5
select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 5