SQL Server update and replace values with values from another table - sql

I have two tables:
Connections:
id user_id connection_info
------------------------------
1 1 ...
2 1 ...
3 2 ...
Lists:
id connection_id name
-----------------------
1 1 ...
2 2 ...
3 1 ...
I currently have user_id's in the lists.connection_id column. I would like to join the lists table with the connections table by connections.user_id = lists.connection_id and then replace the lists.connection_id with the corresponding id from the connections table.

You could use UPDATE FROM like this:
update l
set l.connection_id = c.id
from connections c join lists l on c.user_id = l.connection_id
Initially you would want to test what you are going to update, running a SELECT statement:
select l.connection_id as con_old
, c.id as con_new
, ... (other cols you might want to check)
from connections c join lists l on c.user_id = l.connection_id

Related

SELECT from 2 differents tables

I would like to select all possible brands for different products where the fk_category_id is for example equal to "2".
produits :
id titre fk_category_id fk_marque_id is_active is_delete
1 Swoke 2 1 1 0
2 Café 2 2 1 0
3 Fraise 2 3 1 0
4 Fruits 2 4 1 0
manufacturers :
id name
1 Swoke
2 Liqua
3 Alfaliquid
4 TJuice
5 otherBrands
I already tried a lot of things and for example this request :
SELECT m.name, m.id, p.fk_category_id
FROM produits p
INNER JOIN manufacturers m
WHERE p.fk_category_id = 2
AND p.is_active = 1
AND p.fk_marque_id = m.id
AND p.is_delete = 0;
But it doesn't works.
The expected result is :
result :
id name
1 Swoke
2 Liqua
3 Alfaliquid
4 TJuice
It's the same as the table "manufacturers" but I have to sort by fk_category_id because I only want the brand with the fk_category = 2.
So if someone could explain me or help me to understand how to solve my "problem" ? Thanks you in advance, I continue my research by my side :).
If you need something else i can give you anything.
I think what you need to do is have a condition on your join, to say how the data from the 2 table should join together.
On the assumption that fk_marque_id is your reference in produits to an item in manufacturers (assumed from looking at your where clause), your sql could look like this:
SELECT
p.id, p.titre, m.name, m.id, p.fk_category_id
FROM
produits p
INNER JOIN manufacturers m ON p.fk_marque_id = m.id
WHERE p.fk_category_id = 2
AND p.is_active = 1
AND p.is_delete = 0;
The naming convention of your fields in produits is a little weird however, if one of the the FK fields is a link to an ID in manufacturers. You'd normally expect to see something like FK_manufacturers_Id so it's clear that this column is the reference to that field (Id) in that table (manufacturers)
If you are just looking to join products and manufacturers table where fk_category_id = 2, it can be done something like this
SELECT m.id, m.name
FROM manufacturers m
INNER JOIN produits p
ON m.id = p.fk_marque_id
WHERE p.fk_category_id = 2
AND p.is_active = 1
AND p.is_delete = 0;

How to join a column to another column based on two conditions

I am at a bit of a loss why this is not working/updating. I have a 'sites' table and a 'visit' table. The 'sites' table contains all the unique 'sites' with in the DB and the 'visit' table contains all the unique visits to all the sites.
Site
site_id
region
site
1
a
a
2
a
b
3
b
a
4
b
b
Visit
visit_id
region
site
date
site_id
1
a
a
1
2
a
b
2
3
b
a
3
4
b
b
2
5
c
a
3
6
c
b
1
7
d
a
2
8
d
b
2
When region and site equal each other I want the site_id to populate in the 'visit' table. Here is what I have tried. It does not throw and error but nothing populates in the visit.site_id.
update visit a
set site_id =
(select s.site_id
from site s
where (s.region, s.site) = (a.region, a.site));
Your way has a problem and if even it works, it does not work correctly because if there are the same tuples in both tables your query will update all of them.
update visit a
set site_id =
(select s.site_id
from sites s
where (s.region, s.site) = (a.region, a.site) and a.region = a.site);
rewriting in another way:
update visit a
set site_id =
(select s.site_id
from sites s
where s.region = a.region and s.site = a.site and a.region = a.site);
Here's the result(my PostgreSQL version is 13):

How to select data from table with big rows where column like column1%column2

I have a table Product with 240 000 rows.
I want to select from this table data where idproduct = idproductcomponent
Output makes a table with 3 columns A12345678 35655455952625 9638520963258960, so in different column.
Always idproductcomponent of idproducttype 1 is 0, and idproductcomponent of idproducttype 2,3 are the same of idproduct of idproducttype 1.
Pelase, can you share with me any idea for this select ?
Assuming your database is SQL Server (you don't say which one it is) the query could look like:
select
a.name,
b.name,
c.name
from t a
left join t b on b.idproductcomponent = a.idproduct and b.idproducttype = 2
left join t c on c.idproductcomponent = a.idproduct and c.idproducttype = 3
where a.idproducttype = 1
and a.idproduct = 11163 -- parameter you are searching for
To increase the performance of this query you can add the index:
create index ix1 on t (idproductcomponent);

SQL Code to get results when there are no records in a table

There are 4 tables (user, user options, option type 1, and option type 2). The user table is not directly involved here but mentioned for completeness. Here are the table columns for each of the involved tables.
User Options:
UserID,
Option1ID,
Option2ID,
Option1:
Option1ID,
Option1Description
Option2:
Option2ID,
Option2Description
All the values for Option1 can be combined with all the values for Option2 hence if there a 'x' number of Option1 values and 'y' number of Option2 values then the resultant number of option combinations is 'x' * 'y'. I want to write a query such that it will always result in 'x' * 'y' rows for a given UserID even if there is no record for a particular combination of Option1ID and Option2ID for that user in the UserOptions table. Additionally there should be a column that indicates if the particular combination of Option1 and Option2 exists in the UserOptions table.
Option1
Option1ID Option1Description
----------------------------------
1 1_Description1
2 1_Description2
Options2
Option2ID Option2Description
----------------------------------
1 2_Description1
2 2_Description2
UserOptions
UserID Option1ID Option2ID
---------------------------------
1 1 2
1 2 2
Result
UserID Option1ID Option2ID Exists
----------------------------------------------
1 1 1 0
1 1 2 1
1 2 1 0
1 2 2 1
Given the above what would the SQL query be? In addition note that UserID = 2 does not exist in the UsersOptions table. In this case the query should still return 4 records where the UserID column will always be 2, the OptionID columns will be the same, and the Exists column will always be 0.
You can cross join the users table with the two options tables to generate all possible combinations, then search for a match in bridge table useroptions with a left join:
select u.userid, o1.option1id, o2.option2id,
case when uo.userid is null then 0 else 1 end as uo_exists
from users u
cross join option1 o1
cross join option2 o2
left join useroptions uo
on uo.userid = u.id
and uo.option1id = o1.option1id
and uo.option2id = o1.option2id
You could also use exists instead of a left join:
select u.userid, o1.option1id, o2.option2id,
case when exists (
select 1
from useroptions uo
where uo.userid = u.id and uo.option1id = o1.option1id and uo.option2id = o1.option2iduo.userid
) then 1 else 0 end as uo_exists
from users u
cross join option1 o1
cross join option2 o2

SQL select column equivalence

I have a table that saves the possible states of other tables (entities).
But now I need to find equivalence of states between two entities.
The table structure is something like this
ID TableID StateValue StateDefinition StateDescription
================================================================
1 1 1 Created Just created
2 1 2 Dropped Just Dropped
3 2 1 Created Just Created
4 2 2 Aproved Passed the revision
5 2 3 Dropped Just dropped
I want to get equivalent (comparing text of state) which as a result get this:
TableID1 StateValue1 TableID2 StateValue2 StateDefinition
=============================================================================
1 1 2 1 Created
1 2 2 3 Dropped
My question is, how can it be done??
Do a self join on the table.
A general case might look like:
SELECT A.TableID as TableId1,
A.StateValue as StateValue1,
B.TableId as TableId2,
B.StateValue as StateValue2,
A.StateDefinition
FROM
Table A
INNER JOIN Table B
ON (A.TableId <> B.TableId and A.StateDefiniton = B.StateDefinition)
select t1.TableID as TableID1,
t1.StateValue as StateValue1,
t2.TableID as TableID2,
t2.StateValue as StateValue2,
t1.StateDefinition
from MyTable t1
inner join MyTable t2 on t1.TableID = 1 and t2.TableID = 2
where t1.StateValue = t2.StateValue
and t1.StateDefinition = t2.StateDefinition