SQL Matching Rows - mysql

I am in trouble matching a value in other table and get the corresponding value. I have two tables, table one for product and table 2 for part, I need to match part no from second table with oe number of first table and get SKU.
The problem is OE Number is a bunch of numbers and i need to use something like instr. For Example the table look like this.
Product Table
OeNum | SKU
123-546-625 | A001
568-623-855 | A002
Part Table
PArtid
623
625
for example I need to check if any row in OENUM have the string like part number and if there is one return the SKU. In above example partid 623 is in the second row of oenumber so it return value as
partID | SKU
623 | A002
625 | A001
i am using the query as :
select s.sku, p.id
from sk s, part p
where instr(s.oenum,p.id)>0;
but it doesn't seems to be working.
Please help.

If there are not very much data in your tables I can offer you to try something like:
select s.sku, p.id from sk s join part p on s.oenum like '%'+p.id+'%'

Related

Make a query to combine one result from two different tables with same / different columns

I am trying to make single query for a single result from two different tables. This table has same columns and also different columns.
Table Deposits
Name
D_Currency
D_Amount
Datetime
Michael
USD
500
2022-12-10 09:12:45
Susan
JPY
10000
2022-12-15 12:34:11
John
KRW
7000
2022-12-13 15:21:08
Table Withdrawal
Name
W_Currency
W_Amount
Datetime
Michael
USD
500
2022-12-11 09:55:32
Susan
EUR
800
2022-12-17 11:23:45
John
USD
300
2022-12-13 13:33:43
Result I want:
Name
D_Currency
D_Amount
W_Currency
W_Amount
Datetime
Michael
USD
500
2022-12-10 09:12:45
Michael
USD
500
2022-12-11 09:55:32
John
USD
300
2022-12-13 13:33:43
John
KRW
7000
2022-12-13 15:21:08
Susan
JPY
10000
2022-12-15 12:34:11
Susan
EUR
800
2022-12-17 11:23:45
I want all the columns from both tables, but the order is in Datetime.
I think I can use column Name for join, but not sure how to add Datetime.
First I tried join, which didn't work:
SELECT d.Name, d.D_currency, d.D_amount, w.W_currency, w.W_amount,
d.Datetime as date, w.Datetime as date
FROM Deposits d
JOIN Withdrawal w on w.Name = d.Name
ORDER BY date
I was thinking of UNION, but don't have the slightest idea how to even use it.
Please help.
In order to use the UNION operation, the requirement is that you need to have the exact same schema for the two tables you're applying it on. You can use the NULL values to fill the fields whose value you don't have, and rename the field with the corresponding field name you have in the expected output table.
SELECT Name, D_Currency, D_Amount,
NULL AS W_Currency, NULL AS W_Amount,
Datetime_
FROM Deposits
UNION ALL
SELECT Name, NULL AS D_Currency, NULL AS D_Amount,
W_Currency, W_Amount,
Datetime_
FROM Withdrawal
ORDER BY Name
Some remarks to take into account when applying a union operation:
Applying the union between two sets as we know it, is carried out by the UNION ALL operation. The difference between UNION and UNION ALL is that the former applies an aggregation to eliminate the duplicates too, if you have any. Since in your case you're not supposed to have duplicates, you can avoid stacking up an unnecessary operation.
The order of the fields over the two table you're carrying the union on is important. If the two tables don't have the same fields in the same order, the DBMS will throw a schema mismatch error.
The last ORDER BY clause will apply on the whole output of the union operation.
Check the demo here.

How do compare a single value from one table to another list of values in another table

I have two tables and I would like to compare one value to see if it is less than another value.
Considering the two example tables I want to make a SELECT statement that would tell me, given my wallet amount which items I could afford. How do I say:
SELECT product FROM Store WHERE price < amount
The above obviously does not work I have searched everywhere.
Wallet:
name amount
--------------
Mymoney 20
Store:
product | price
-----------------------
Apple | 3
Orange | 4
Steak | 21
As you are working with 2 diferent tables, you should use some kind of join like:
select product from Store inner join Wallet on price <= Mymoney
sqlFiddle:
http://sqlfiddle.com/#!9/b431a2/4

sum function is returning wrong value

I have 3 tables
1.Franchiese
Id Name
1 Vivek
2.Purchase
Id Fran_id commission_amount
1 1 100
2 1 1
3.Fran_payment
Id Fran_id amount
1 1 50
My SQL Query is
select franchiese.id,franchiese.name,sum(fran_payment.amount) as paid,sum(purchase.commission_amount) as tot,sum(purchase.commission_amount)-sum(fran_payment.amount) as rem from franchiese left join fran_payment on franchiese.id=fran_payment.fran_id left join purchase on franchiese.id=purchase.fran_id
It's giving me
Id Name Tot Paid Rem
1 vivek 101 100 1
Expected Answer
Id Name Tot Paid Rem
1 vivek 101 50 51
I think you have two identical entries in the Fran_payment table. Your SQL statement should work as intended, and is giving you logically correct values, but I think you have unexpected data in your table.
You are joining 3 tables which have unequal number of rows. Purchase table has 2 rows, while fran_payment has only one. At the time of join, the row in fran_payment is repeated to match the number of rows in purchase. Hence the row is duplicated and sum becomes 50 + 50 = 100 and your data would look like something like this-
ID | Name | fran_payment.amount | purchase.comission_amount
1 | Vivek | 50 | 100
1 | Vivek | 50 | 1
Try something like this
Select fran_id, sum(fran_payment.amount) as paid from purchase;
This should work.
Also, you'll need to run a sub query to only fetch data for given entry. Or, normal sum function would return the sum of while column, irrespective of the ID.
Select id, sum(fran_payment.amount from fran_payment where fran_payment.fran_id = id) as paid from franchise;
I hope that works. All the best.
PS: It's franchise, not franchiese.

How to return data from two tables only when the column value in one table is same as another table using MySQL?

I'm new to MySQL and I want to return data from two tables only when the Mobile numbers in both the table are same. I don't want any null values. How can I achieve this?
Example:
Table A
UserID CandidateName CurrentMobile CurrentDistrict Email Centre
1 Max 98234 Chennai aaa#a.com A
2 Raju 97364 Salem bbb#b.com B
3 Ramesh 99873 Trichy ccc#c.com C
Table B
Name MobileNumber District
Maximus 98234 Salem
Ramesh 99873 Trichy
Venkat 98376 Chennai
I want the following result:
UserID CandidateName Name CurrentDistrict District Email Centre MobileNumber
1 Max Maximus Chennai Salem aaa#a.com A 98234
3 Ramesh Ramesh Trichy Trichy ccc#c.com C 99873
I tried to use UNION, but it gives null values in some columns and also returns all the data from both the tables.
You want to join the two tables, on the condition that A.CurrentMobile is the same as B.MobileNumber:
SELECT
A.UserID,
A.CandidateName,
B.Name,
A.CurrentDistrict,
B.District,
A.Email,
A.Centre,
B.MobileNumber
FROM
A,
B
WHERE
A.CurrentMobile = B.MobileNumber
Alternatively you can use this syntax for the same result:
SELECT
... (all the same fields again)
FROM
A
INNER JOIN B ON A.CurrentMobile = B.MobileNumber
The query will be much faster if you have an index on the two fields you use for the join (that is, the mobile number fields). Read more about joins here and here.

SQL Database with undefined columns

I'm working on a project where I'm dumping data into a mysql database about different people, however, each person can have many entries so I'm not sure how to have a lot of columns.
e.g
Name id
jack 234 01241990 13241990 03451993 10945
james 222 01131998 14242001 03414235 10945435 3456363 3465758
jill 1234 01131998 14242001 03414235 10945435 3456363 3465758 4253156316 6427247 583583
As you can see there can be many entries for each person, not in 100's, but I think the max can be around 20-30ish? So how do I build a database that I can insert values into without knowing how many entries will be per person, beforehand.
I am using perl script to insert values. Any ideas will be helpful
EDIT: People are suggesting to create two tables, however, when I joint he tables, I want one row for each person.
e.g After joining my view should look like
james 222 01131998 14242001 03414235 10945435 3456363 3465758
My suggestion would be to split the data into two tables (People and Data):
People:
NAME ID
Jack 234
James 222
Jill 1234
Data:
ID PeopleID Data
1 234 01241990
2 234 13241990
.
.
99 1234 6427247
100 1234 583583
You can then use joins to get the data for each person
SELECT p.Name,
p.ID,
d.Data
FROM People p
JOIN Data d
ON d.PeopleID = p.ID
ORDER BY p.Name --(assuming you want names in alphabetical order)
You should get something like the following
Name ID Data
Jack 234 01241990
Jack 234 13241990
.
.
Jill 1234 6427247
Jill 1234 583583