Table1:
id name value
===================
1 Jane 28
2 Joe 35
Table2:
id name value integer
=========================
1 Jane 28 379
2 Joe 30 325
2 Joe 32 380
1 Jane 28 385
Table1 contains information about user: ID, Name and Value.
Table2 contains information about changes: ID, Name, Value and Integer.
Integer is a value thats always increase and changes about twice a day.
I want to transfer the value from table1 for a specific ID and that value to table2 and put that value at the last line.
In this example I want
ID: 2
Value: 35
in Table1 to relace
ID: 2
Value: 32
in Table2. And I need to use the Integer in some way..
If interger the column which tells the latest in the table2 then you can use the update join as
update table1 t1
join table2 t2 on t1.id = t2.id
join (
select max(`integer`) as `integer`,id
from table2
group by id
)x
on x.id = t2.id and x.`integer` = t2.`integer`
set t1.value = t2.value
where t1.id = 2 ;
Related
I have a table T1 with standard columns like name, email, phone_number. The custom fields are store in custom_fields table as key, value.
I need to have key name of custom_fields as column name on actual table. How can I achieve this
Table T1
id name email
1 John john#example.com
2 Sam same#mydomain.com
custom_fields
id T1_id key value
1 1 Age 32
2 1 Job Title CEO
3 2 Age 40
4 2 Car Owned Ford EcoSport
Required
T1_id name email age job_title car_owned
1 John john#example.com 32 CEO Not specified
2 Sam same#mydomain.com 40 Not specified Ford EcoSport
You should use the table custom_fields two time one for job and one for car
select t1.id, t1.name, t1.email, t2.value, t3.value
from t1
inner join custom_fields t2 on t1.id = t2.t1_id and t2.key = 'Job Title'
inner join custom_fields t3 on t1.id = t3.t1_id and t3.key = 'Car Owned'
I have 3 tables:-
table1 :-
ReportType | ResourceId
t2 123
t3 5
table2:-
Id | Name | Created
1 A 10
2 B 11
123 C 12
table3:-
Id | Name | Created
4 D 13
5 E 14
6 F 15
table1's ResourceId and table2 and 3's Id column have same values
I want to create a 4th table like this:-
ReportType | ResourceId | Name | Created
t2 123 C 12
t3 5 E 14
such that wherever table1's ReportType is t2 I want the Name and Created value from table2 for the condition table1.ResourceId = table2.Id and wherever table1's ResourceType is t3 I want the Name and Created value from table3 for the condition table1.ResourceId = table3.Id.
PS: This isn't some sort of HomeWork. I have been stuck at this query for the past 1 hour, I have read various answers and tried a few queries of my own before posting the question. Any help would really be appreciated.
Explanation in comments :)
--here we join first and second table, but we filter results to include only ReportType = t2
select t1.ReportType, t1.ResourceId, t2.Name, t2.Created from table1 as t1 join table2 as t2 on t1.ResourceId = t2.id
where t1.ReportType = 't2'
union all
--here we join first and third table, but we filter results to include only ReportType = t3
select t1.ReportType, t1.ResourceId, t3.Name, t3.Created from table1 as t1 join table3 as t3 on t1.ResourceId = t3.id
where t1.ReportType = 't3'
You can use the below query:
select report_type, resourceid,name, created from dbo.t2, dbo.t1
where report_type='t2' and ResourceId=id
UNION
select report_type, resourceid,name, created from dbo.t3, dbo.t1
where report_type='t3' and ResourceId=id;
I need to get all the records where table1.user is in table2.userid and table2.country = 8 and table2.status = 1
This is the sample data in my database
table1
id user
-- ----
1 12
2 23
3 34
4 32
5 85
6 38
table2
id userid country_id status
-- ---- ----- ----
1 12 5 1
2 12 8 1
3 85 8 1
4 38 8 0
5 38 7 1
6 23 8 1
7 23 4 1
in this case I should only get the id #3 in table2
Inner join will do the job taking only record with match in the two tables.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 on t1.user = t2.userid
WHERE t2.country=8 and t2.status=1
You mean
"get all the records where table1.user is in table2.userid
with table2.country = 8 and table2.status = 1 ?"
If so, then:
Select * from table1 t1
Where Exists(Select * from table2
Where userId = t1.user
and country = 8
and status = 1)
Well to accomplish that is quite simple, you just need to make use of an inner join. An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
In your case, the INNER JOIN will return all rows from table2 where the join condition is met.
For a more detailed explanation and examples just visit this link: http://www.w3schools.com/sql/sql_join.asp
based on your response to Adam's approach "
I tried this but it didn't work, yes it gets the records but the id #2 in table2 is also included. what I need to get is the id #3 in table2. Thanks :) ". I changed my SQL query to:
SELECT
*
FROM
table1
INNER JOIN
table2
ON
table1.user = table2.userid
WHERE
table2.userid = 85
I assumed you only want the data of that particular userid in table2
Can we use a join operation and make restrictions for different columns at the same time? I want to create a table on a Database (called DB1 in the example) based on three tables from another database (called DB2 in the example) where some columns in the new table filled with entrys when there´s a specific entry in an other column (in the example a"4" in column "attributeID" at table 2 indicates an entry in column "gender", a "5" indicates an entry in column "age"; a"7" in column "attributeID"at table 3 indicates an entry in column "street"). -> If yes, then how to do ?
Both databases are on the same server and DBMS is the same. ID1 and ID2 based on table1 in DB2; ID2, attributeID and value (where the gender or age is written depending on entry in attributeID) based on table2 in DB2; ID2 attributeID and value (where the street is written when the entry in the attributeID is a 7)based on table3 in DB2 .
Sample Data:
Table 1:
ID1 ID2
1 2
2 4
3 5
Table 2:
ID2 attributeID value
2 3 Kahtrin ->an example entry which is not relevant
2 4 miss
2 5 22
4 1 active ->an example entry which is not relevant
4 4 EMPTY/NULL
4 5 47
5 4 mr
5 5 34
5 6 Hindu ->an example entry which is not relevant
Table 3
ID2 attributeID value
2 5 20% ->an example entry which is not relevant
2 7 middlestreet 40
4 4 chinese ->an example entry which is not relevant
4 7 churchstreet 12
5 3 3Euro
5 7 EMPTY/NULL
Expected Outcome
Table 4:
ID1 ID2 gender age street
1 2 miss 22 middlestreet 40
2 4 47 churchstreet 12
3 5 mr 34
Here´s what I tried out (Made from point of view that I´m using DB1):
INSERT INTO table4
(id1,
id2,
gender,
age,
street)
SELECT t1.id1,
t1.id2,
t2.value
t2.value
t3.value
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID2=t2.ID2
AND value = 4 OR 5
LEFT JOIN db1.table3 t3
ON t1.ID2=t3.ID2
AND value = 7;
You select is wrong (don't produce the expected result ) you should use two time the table t2 using an alias
and you should explicit assign the table t3 to value
SELECT t1.id1,
t1.id2,
t2.value
t4.value
t3.value
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID2=t2.ID2
AND value = 4
LEFT JOIN db1.table2 t4
ON t1.ID2=t4.ID2
AND value = 5
LEFT JOIN db1.table3 t3
ON t1.ID2=t3.ID2
AND t3.value = 7;
I got 2 tables.
TABLE 1
ID FRANCHISENAME TELEPHONE FRANCHISE_ID
1 BURGER 666-555-999 5
2 JSUBS 666-555-999 7
3 STEAKS 777-888-999 3
TABLE 2
ID NAME TELEPHONE EMAIL FRANCHISE_ ID
5 JOHN 555-444-333 JOHN#GMAIL.COM 5
5 JOHN 555-444-333 JOHN#GMAIL.COM 7
6 EDGARD 555-444-333 EDGARD#GMAIL.COM 9
I want to retrieve all data in table one, except for that data where the user has his email in Table 2. As for example JOHN has franchise_id 5 and 7, so the query would only return
3 STEAKS, 777-888-999, 3
Assuming that TABLE_1 & TABLE_2 relate to each other through TABLE_1.FRANCHISE_ID & TABLE_2.FRANCHISE_ID
You can use NOT EXISTS
SELECT
*
FROM TABLE_1 T1
WHERE NOT EXISTS(
SELECT *
FROM TABLE_2 T2
WHERE T2.FRANCHISE_ID = T1.FRANCHISE_ID
AND T2.EMAIL = 'JOHN#GMAIL.COM'
)
OR
You can use LEFT JOIN along with IS NULL
SELECT
T1.*
FROM TABLE_1 T1
LEFT JOIN TABLE_2 T2 ON T1.FRANCHISE_ID = T2.FRANCHISE_ID
WHERE T2.FRANCHISE_ID IS NULL;
SELECT t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2
ON t1.FRANCHISE_ID = t2.FRANCHISE_ID
AND LEN(IFNULL(t2.EMAIL,'')) > 0
WHERE
t2.ID IS NULL
Even if there is a record in Table 2 if it has no email it will be returned by this query. You can expand say to > 7 or more to check for a minimum level of validity of email address.
This should get you there using the NOT IN function. It will exclude records from Table 1 if there is a matching Franchise ID in Table 2, unless the email field is Table 2 is null:
SELECT * FROM Table1
WHERE Table1.Franchise_ID NOT IN
(SELECT Table2.Franchise_ID FROM Table2
WHERE Table2.Email IS NOT NULL);