Example I have table 1:
ID USER Password
1 name1 pass1
2 name2 pass2
Table 2 is blank but have columns:
ListID FNAME LNAME
null null null
null null null
I want to pass the record from table1 to table 2 or join them together so every record inserted to table 1 will be inserted to table 2 as well.
how about adding an relational id's in both table and truncate it..
Related
I've been trying to wrap my brain around this using joins, subquery joins, not exists clauses and I keep failing to come up with a query that produces the correct results.
Table A - PRIMARY id (irrelevant for this issue)
id | campaign_id | user_id
--------------------------
1 1 1
2 1 2
3 0 3
4 2 3
5 1 2
Table B - UNIQUE campaign_id+user_id
campaign_id | user_id | admin
-----------------------------
1 1 1
1 2 0
1 3 0
2 3 0
What I need to do is find instances of Table B where the user no longer has an entry in Table A that correspond with the campaign_id in Table B. Table A is the main content and they can have multiple entries of Table A that are present in the campaign. Table B is a member table that indicates they're a member of the campaign and whether they're an admin or not. In addition, they could have in entry in Table B as admin, but not have an entry in Table A, so the query must check for admin=0.
In the example entries, the invalid entry in Table B would be campaign_id 1, user_id 3
Use an outer join and then state in the where clause that the outer joined table's user_id is null:
select tblB.*
from tblB
left join tblA
on tblA.campaign_id = tblB.campaign_id
and tblA.user_id = tblB.user_id
where tblB.admin = 0
and tblA.user_id is null
1st Table - explore_offers:
- id
- Primary Key - offer_unique
2nd Table - participated_explore_offers:
- id
- email - user_email
- Primary Key - offer_unique
What i want:
* Show 1st Table records, and exclude those records, which are in the 2nd table with a specific email found
ex:
SELECT eo.*
, peo.user_email
FROM explore_offers eo
LEFT
JOIN participated_explore_offers peo
ON eo.offer_unique = peo.offer_unique
WHERE peo.user_email = 'test#gmail.com'
I've tried that example, and i'm getting 0 records.
I have 2 records in the first table, and one in the second table, and the result i want is:
*. get that one record from the first table, where this record does NOT exist in the second table.
1st Table content:
Nr id Primary Key
1 0 m1
2 1 m2
2nd Table Content
Nr id user_email Primary Key
1 0 test#gmail.com m1
1 0 test2#gmail.com m2
Expected
Nr id Primary Key
1 1 m2
What i had:
0 Records
SQL DEMO
Try this :
select * from explore_offers
where offer_unique not in
(select offer_unique from participated_explore_offers where user_email='test#gmail.com')
Move the email filteration to the JOIN condition to make it work with LEFT JOIN:
SELECT eo.*,peo.user_email
FROM explore_offers eo
LEFT JOIN participated_explore_offers peo ON (eo.offer_unique = peo.offer_unique)
AND peo.user_email = 'test#gmail.com'
WHERE peo.user_email is null;
demo:
| Nr | id | offer_unique | user_email |
|----|----|--------------|------------|
| 2 | 1 | m2 | (null) |
I have a table named Users.
I import some users from other table.
they have a parent_id
my table i now
id,parent_id,imported_rows_id
1,1,NULL ->my old data has not last row value
2,1,Null ->my old data has not last row value
3,1,1100
4,1100,1101
5,1100,1102
6,1102,1103
Now i want to change all parent_id to id where imported_rows_id = parent_id
same as here:
3,1,1100
4,3,1101
5,3,1102
6,5,1103
update users set parent_id = (select id from users where parent_id=imported_rows_id)
Not allow on the same table
Sincerely
You can do it with a self join:
update TableName t1 join
TableName t2 on t1.imported_rows_id=t2.parent_id
set t2.parent_id=t1.id
where t2.imported_rows_id is not null
Result:
id parent_id imported_rows_id
--------------------------------
1 1 (null)
2 1 (null)
3 1 1100
4 3 1101
5 3 1102
6 5 1103
Result in SQL Fiddle
I'm trying to join distinct ID's from a subquery in a FROM onto a table which has the same ID's, but non-distinct as they are repeated to create a whole entity. How can one do this? All of my tries are continuously amounting to single ID's in the non-distinct-id-table.
For example:
Table 1
ID val_string val_int val_datetime
1 null 3435 null
1 bla null null
1 null null 2013-08-27
2 null 428 null
2 blob null null
2 null null 2013-08-30
etc. etc. etc.
Virtual "v_table" from SubQuery
ID
1
2
Now, if I create the query along the lines of:
SELECT t.ID, t.val_string, t.val_int, t.val_datetime
FROM table1 AS t
JOIN (subquery) AS v_table
ON t.ID = v_table.ID
I get the result:
Result Table:
ID val_string val_int val_datetime
1 null 3436 null
2 null 428 null
What I'd like is to see the whole of Table 1 based on this example. (Actual query has some more parameters, but this is the issue I'm stuck on).
How would I go about making sure that I get everything from Table 1 where the ID's match the ID's from a virtual table?
SELECT t.ID, t.val_string, t.val_int, t.val_datetime
FROM table1 AS t
LEFT JOIN (subquery) AS v_table
ON t.ID = v_table.ID
Sample fiddle
I want to fetch a data from two tables even if the second table has no data. I need empty values for no data.
For e.g. table 1 has the fields
id
name
password
table 2 has
id(foreign key refer id from table 1)
f1
f2
f3
Some time table 2 doesn't have the value for a particular user. For that I need the null values.
Here I give sample input and expected output
table1
id name password
1 nam1 pass1
2 nam2 pass2
table 2
id f1 f2 f3
1 1 2 3
sample output
id name password f1 f2 f3
1 nam1 pass1 1 2 3
2 nam2 pass2 null null null
I need a query to fetch a data.
You need SELECT with LEFT JOIN
Sample query will be like:
SELECT
table1.id,table1.name,table1.password,table2.f1,
table2.f2,table2.f3
FROM table1
LEFT JOIN table2
ON table1.id=table2.id
http://www.w3schools.com/sql/sql_join_left.asp
You want to look to left join and right join