Select data from multiple tables in mysql database - mysql

I have a database with multiple tables that I am selecting from. In some tables I have 2 entries fr the same case and while selecting I want to retrieve both those values. My issue is if out of 8 tables 2 tables have two entries for a column then in the final output i see 4 rows for the same case with columns populated from all the tables which makes it difficult to know which columns have two entries.
Example
Table1:
case_id company_id date location_type
1 123 08-07-2020 rest1
2 234 01-01-1999 rest2
3 456 01-01-1995 rest1
Table2:
Case_id type Series
1 A ABCD
1 A GEFT
2 B TRFG
3 A HJKL
Table3:
Case_id type Series
1 A UIPO
2 A GGTH
3 B TYUI
The query:
SELECT Table1.company_id,"|",Table2.type, Table3.type FROM Table1
LEFT JOIN Table2 ON Table1.case_id = Table2.case_id
LEFT JOIN Table3 ON Table1.case_id = Table3.case_id
where Table1.location_type = "rest1"
This is what I get:
company_id type type
123 A A
123 A A
456 A B
How do I get:
company_id type type
123 A A
123 A NULL
456 A B

Related

How to join domain table with second table using two foreign keys for one primary key?

I have two tables as follows:
TABLE1 (domain table):
ITEM_ID (PK)
ITEM_NAME
ITEM_PRICE
1
Orange
123
2
Apple
321
3
Banana
131
4
Melon
312
TABLE2:
CART_ID (PK)
CART_NAME
ITEM1_FK (FK1)
ITEM2_FK (FK2)
11
Cart11
1
3
16
Cart16
2
2
85
Cart85
3
4
96
Cart96
4
1
Both FKs from TABLE2 points to TABLE1's PK. My question is how can I create a SELECT statement that joins the tables above into something like that:
CART_ID (PK)
CART_NAME
ITEM1_NAME
ITEM1_PRICE
ITEM2_NAME
ITEM2_PRICE
11
Cart11
Orange
123
Banana
131
I tried using INNER JOIN operations with both the FKs, but I always get ambiguous column name error or manage to create a table in which both "ITEM_NAME" and "ITEM_PRICE" fields have the same value.
You need to alias the tables and join them twice. Try something like:
SELECT t.cart_id,
t.cart_name,
a.item_name item1_name,
a.item_price item1_price,
b.item_name item2_name,
b.item_price item2_price
FROM table2 t
INNER JOIN table1 a
ON t.item1_fk = a.item_id
INNER JOIN table1 b
ON t.item2_fk = b.item_id
;
That said, this isn't a great table design, considering it has the number of items in a cart hard-coded, but that's a different story.

write query using joins using condition

there arr two tables table1,table2.
table1:
number
type
name
1100
1
steve
1100
2
john
1100
2
jack
.....
and so on for different numbers
similiarly table2:
number
type
name
1100
1
abraham
1100
2
john
1100
2
jack
and so on for different numbers...
Expected output:join two tables based on equal number and equal type but on name condition it should print where there is mismatch.for example in above john had type '2' in table 1 and in table 2 also there is john but for type '1' it is mismatch as steve <> abraham.I am doing this query but the output contains rows with jack and john too:
number
type
name1
name2
1100
1
steve
abraham
1100
2
john
jack
1100
2
jack
john
here the number of rows in table 1 and table 2 are equal similiarly number of rows for each type in each table are also equal
select * from table1,table2 where table1.number=table2.number and table1.type=table2.type and table1.name <> table2.name
expected output:
number
type
name1
name2
1100
1
steve
abraham
As you can see above I have given the output which I got for my query(it contains jack,John pairs but I don't want them as for type '2' names are same in both tables)but if you see for type '1' names are different in both tables so I want to print them.if names are different in type'2' for both tables I want them too as output but here in example names are same in both tables for type '2'.Can anyone help me..?
I think this will do what you want.
SELECT table1.number
,table1.type
,table1.name
,table2.name
FROM table1
JOIN table2 ON
table2.number = table1.number AND
table2.type = table1.type AND
table1.name NOT IN (SELECT t2.name
FROM table2 t2
WHERE t2.number = table1.number AND
t2.type = table1.type)

Mysql update row value in table based on different row

I have the following table structure
TABLE A
Productid price groupId
1 100 A
2 99 A
3 0 A
4 50 B
5 49 B
6 0 B
I populate table A with prices from table B joining on Id. Sometimes table B doesn't have prices.
In cases were b doesn't have price I want to update the price to be another price from that same group, as I can't have a price of zero.
Is there an way to update table a price column using itself based on group? e.g. update productId 3 price to be the price of another product in it's group (1 or 2)
TABLE A after update
Productid price groupId
1 100 A
2 99 A
3 100 A
4 50 B
5 49 B
6 49 B
It seems silly but these are the business rules (it makes sense irl I simplified the problem for the example)
When I tried the following I got Error:
update 'Table A' t1
join (select price ,groupId from 'table A' where Price > 0 group by
groupId) as t2
on t1.groupId = t2.GroupId
SET t1.Price = t2.Price
(conn=58292) Can't reopen table: 'Table A'
I've thought of creating a third temporary table but that seems.... wrong? I am sure there must be a way to do this using update statement and joins
I would phrase the query as:
update tablea a
inner join (select groupId, max(price) price from tablea group by groupId) a1
on a1.groupId = a.groupId
set a.price = a1.price
where a.price = 0 and a1.price > 0
Notes:
the table name should be surrounded with single quotes (those stand for literal strings) - if your table name really contains spaces, then use backticks for quoting (or better, yet, fix the table name!)
I changed the subquery to make it a valid aggregation query - yours has non-aggregated columns that do not belong to the group by clause, which is not a good practice, and might generate errors, depending on the SQL mode of your database
In this demo on DB Fiddlde with your sample data, the content of the table after update is:
Productid | price | groupId
--------: | ----: | :------
1 | 100 | A
2 | 99 | A
3 | 100 | A
4 | 50 | B
5 | 49 | B
6 | 50 | B

Joining on primary key but in one table keys are separated by commas

I'm new to SQL and couldn't find anything on my issue:
This is a simplification of my issue, but basically I have 2 tables I want to join on some column like 'fruit', but table 1 has some rows where fruit is separated by commas in the row, is there a way to join the tables as if the fruit were in different rows for the second table? (I just arbitrarily filled in count)
TABLE 1
count | fruit |
1 | apple, berry
1 | berry
1 | banana
1 | orange, banana
TABLE 2
count | fruit |
1 | apple
4 | berry
15 | banana
11 | orange
This may be help you, Here i have used find in set function in inner join from this you can easily join 2 tables with comma separated values
Table Schema -
CREATE TABLE Table1(
count_val INT,fruit TEXT);
CREATE TABLE Table2(
count_val INT,fruit TEXT);
INSERT INTO Table1(count_val, fruit)
VALUES (1,'apple, berry'),(1,'berry'),(1,'banana'),
(2,'orange, banana');
INSERT INTO Table2(count_val, fruit)
VALUES (1,'apple'),(1,'berry'),(1,'banana'),
(2,'orange');
Sql Query-
SELECT * FROM Table2 AS T2
INNER JOIN Table1 AS T1 ON find_in_set(T2.fruit,T1.fruit)
DB Fiddle Link - Here
You should never join on a text colum, you should always add an ID (Integer, which will be your primary key) to your colum for example
TABLE 1
ID COUNT FRUIT
1 1 apple
2 2 berry
3 3 banana
4 1 orange
And for table 2 the same
for all the rows where you have fruits seperated by a comma you should add an extra row.
then if let's say apple already exists you can either do 2 things, see if the apple exists and increase the count by 1, or simple insert another row with apple.
then if you want to join the 2 tables
SELECT T1.count, T1.fruit, T2.count, T2.fruit
FROM table1 T1
INNER JOIN table2 T2 ON T1.id = T2.id;
If you want to count the amount of a particular food use SUM() functin in SQL

Select entries from table B with multiple conditions from table A

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