Get data with 2 tables - sql

I have table1 from which i want to get some columns but column id from table1 must be same as column id from table2
Something like this:
Select title, image, price
from table1
where id = id from table2

You could use an inner join
SELECT title,image,price from table1
INNER JOIN table2 ON table1.id = table2.id
I think it's odd that your foreign key on table2 has a name (id) that imply a primary key, unless you want to join two primary keys to create a table partition.

Related

Delete records where count of a related records in another table matches a condition

I have two tables with the following schema:
table1 (
id UUID UNIQUE NOT NULL PRIMARY KEY,
...
);
table2 (
id UUID UNIQUE NOT NULL PRIMARY KEY,
table1_id UUID NOT NULL FOREIGN KEY REFERENCES table1(id),
...
);
I would like to delete all records in table 1 where the count of related records in table 2 (meaning those referencing table1 with a foreign key) equals 1. I'm not entirely sure how to do this.
Here is an invalid query I've made up that expresses what I want to do:
DELETE
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.table1_id
WHERE COUNT t2.table1_id = 1;
I am getting a syntax error with the LEFT JOIN. I'm not certain how to join on a delete.
I am using PostgreSQL 15.
A straightforward option is to filter with a correlated subquery that commutes the count of matching record in table2 for each row of table1:
delete from table1 t1
where 1 = (
select count(*) from table2 t2 where t2.table1_id = t1.id
)
The query should take advantage of the underlying index of the foreign key relationship.

left join where the primary key has both integer and string

I need to join two tables where the primary keys have both integer and string value. When I use left join using id column as primary key I am getting the records related to the integer only.I would like to get the output for both integer and string as shown in the output. Can anyone assist please.
select t1.*
,t2.Position
from t1
left join t2
on t1.id=t2.id;
I am getting the output which related to id (integer) only.I would like to get the output for both integer id and string id.
how to use UNION DISTINCT here – Nrad
SELECT id FROM table1
UNION DISTINCT
SELECT id FROM table2
or
SELECT CAST(id AS CHAR) AS id FROM table2
UNION DISTINCT
SELECT id FROM table1
DISTINCT keyword is optional and may be skipped in both cases.
If you need the ordering shown then add
ORDER BY id + 0 = 0, id
to the end of a query.
i have some other different columns in both table that I need to take. – Nrad
SELECT id,
COALESCE (t1.name, t2.name) name,
t1.salary,
t2.position,
t1.department
FROM ( SELECT id FROM table1
UNION DISTINCT
SELECT id FROM table2 ) t0
LEFT JOIN table1 t1 USING (id)
LEFT JOIN table2 t2 USING (id)

Join tables with Two foreign keys

I am searching for a real scenario problem that I faced last night while joining two tables with foreign keys. Actually I want to get all values from second table on behalf of foreign key.
Here are my two tables let suppose:
table1 (id_user_history(PK),id_user(FK), order_no, p_quantity)
table2 (id_shoping_cart(PK), id_user(FK),order_id, prod_quantity)
Now I want to get all values from table2 by joining these tables with table1(id_user(Fk)) and table2( id_user(FK))
SELECT *
FROM table2 t2
LEFT JOIN
table1 t1
on t1.id_user = t2.id_user
all records from table 2 and only those record which match on table 1.
SQL is mainly set logic. Here's a link which helps visualize.
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Looks like a simple join fits the bill:
select *
from table1 t1
left join
table2 t2
on t1.id_user = t2.id_user

select a column in foreign key

Suppose I have two tables in SQL where
table1 has columns - id, name, sex.
table2 has columns - eid, group, hours.
column 'eid' is the foreign key in table2 and column 'id' is the primary key on table1.
suppose I conduct a search using 'select * from table2 where hour=x' in table 2 and get some results. How can I list the names of the people (from table1) associated with the ids from the search?
GOt it!
SELECT T1.NAME FROM TABLE2 T2
INNER JOIN TABLE1 T1 ON (T1.ID=T2.EID)
WHERE T2.HOUR=X

Query to get name from a table with 2 ID columns

I have 2 tables. The first has a structure like
id (int)
parent_id (int)
category_name (varchar)
The second has a structure like:
id (int)
old_category (int)
new_category (int)
I want to make a query to pull the old category and new category names all in one query for the parents. That is, I want to get the name of the old parent, then get the name of the new parent. The second table contains a historical list of all the parent ID changes made in the first table. How can I do this? Thanks!
SELECT old_cat.category_name, new_cat.category_name
FROM join_table
LEFT JOIN category_table as old_cat
ON old_cat.id = join_table.old_category
LEFT JOIN category_table as new_cat
ON new_cat.id = join_table.new_category;
If you've managed to get this far, it's time to stop coding and start learning MySQL JOIN Syntax.
SELECT
t.id,
t.old_category,
oc.category_name as old_category_name,
t.new_catevory,
nc.category_name as new_category_name
FROM
table1 t
INNER JOIN table2 oc on oc.id = t.old_category
INNER JOIN table2 nc on nc.id = t.new_category
I would seriously consider some modifications in column names, btw. To start: why don't the categories in table2 have an id postfix?
I will take a guess at what you meant:
tblOne
id
cat_name
tblTwo
id
tblOneId (old_cat) int
new_cat int
select * from tblOne t1, tblTwo t2 Where t1.id = t2.tblOneId
Join them?
select * from table1
join table2 j1 on table2.id = table1.old_category
join table2 j2 on table2.id = table1.new_category