join and select operation on two tables - mysql

i got two tables like
table1
=======
id primary
sku
price
vendor
table2
=====
id primary
sku
alt_sku
basically, my search term will be sku or alt_sku, and based on it, I want to select data from table1
what will be best approach to do this? should I create a third table to store relation between table1.sku and table2.sku /table2.alt_sku?? or how can I get data from table1
sorry, if this question is veryy foolish...I am trying to learn mysql, so for, I was on WP and never went to basics

You can try an inner join. Assuming TERM is the sku you are searching for.
SELECT
t1.*
FROM
table1 t1
inner join table2 t2 on (t1.sku = t2.sku)
WHERE
t1.sku = 'TERM'
or t2.alt_sku = 'TERM'

What exactly are you trying to do with Table2? Perhaps a better approach would be the following:
Table1
id
price
vendor
Table2
Table1Id
Sku
That way you can store a 1-N number of SKUs associated with Table1. If you only want 2 potential SKUs, then you could add a SkuType field to your Table2 - 1 for Primary and 2 for Alternate, but that may be going too deep for your needs...
And to query:
SELECT T.*
FROM Table1 T
JOIN Table2 T2 ON T.Id = T2.Table1Id
WHERE T2.Sku = 'SomeSku'

I think your table structure should be like that:
table1
=======
id primary
IdFromtable2
price
vendor
table2
=====
id primary
sku
alt_sku
Your Select Query will be like:
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.IdFromtable2 = t2.Id
WHERE t2.sku = #sku AND
t2.alt_sku = #alt_sku

Basically,The finger rule for table relationships is as following:
If the relations is one-to-many then you should save the PK from the many table as additional column in the one table.
If the relations is many-to-many you should create a third table (connecting table). This table will have both PK from both tables (for each tbl1 PK you will have all tbl2 PK's and vice versa).
That way you preventing duplicated records.

Related

MySql - Finding duplicates and deleting them in two tables

I have a very big amount of data on my tables (table 1 and table 2) so I am asking this simple question because I want to have the most efficient way to do this:
How can I delete all the entries from table 1, which are also in table 2 (identified by attribute 'atribxy'). So there will be only exclusive entries in table 1 and in table 2. How can I achieve this in the most efficent way using a SQL-query?
There are many ways to do so:
Using JOIN
DELETE table2
FROM table2
INNER JOIN table1 ON table1.atribxy = table2.atribxy
Using IN
DELETE FROM table2 WHERE atribxy IN (SELECT atribxy FROM table1)
Using EXIST
DELETE FROM table2 t2 WHERE EXISTS ( SELECT 1 FROM table1 t1 WHERE t1.atribxy = t2.atribxy)

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

SELECT * FROM table1 LEFT JOIN table1.value AS table2

i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view

How to fetch data from two tables in sql

I have two tables:
This is table1:
product_id|product_desc|product_name|product_s_desc
This is table2:
product_price_id|product_id|product_price
Now I want to fetch data from these tables. product_id is same in both tables.
I want to fetch
product_s_desc
product_desc
product_name AND product_price from other table.
Please help me do this.
I'm assuming you have a field named product_price in your second table (you didn't list it):
SELECT t1.product_s_desc, t1.product_desc, t1.product_name, t2.product_price
FROM table1 t1
INNER JOIN table2 t2 ON t2.product_id = t1.product_id
You should check out the MySQL manual regarding JOINS, as this is a very basic part of writing SQL queries. You might also consider adding an index on table2 for the product_id field to make the query run faster.
Select * from table1 join table2 on table1.productid = table2.productid
SELECT t1.*,t2.product_price
FROM table1 t1,table2 t2
WHERE t1.product_id=t2.product_id
$sql = "SELECT Student.First_Name,Student.Last_name,Student.Mobile_No,Student.Email,Student.Institue,Student.DOB,Student.Gender
Address.Address_Line1,Address.City,Address.State,Address.Country,Address.Zip_code
FROM Student INNER JOIN Address
ON Student.Id=Address.Id;";

MYSQLI SQL query over multiple tables fail

I am very new to SQL and i need to retreive a number of attributes from different tables but. I am searching for images and details about a user by searching for them under their name.
For example:
SELECT * FROM table1 WHERE name LIKE '%tim%' AND last_name '%smith%'
I need the following attributes in my result:
I will need all the details from table 1
account id and image from table 2
path from table 4
I have tried
SELECT table1.id table1.name table1.last_name table 2.account_id table4.path FROM table1 table 2 table4 WHERE table4.image_id = table 2.image AND table1.first_name LIKE '%tim%' AND table1.last_name LIKE '%smith%'
I am considering spilting this up into multiple queries which would solve this issue, however I am sure this can be done under a single query?, I am just not sure how to structre it, I have tried a number of times, but all queries fail in phpmyadmin
Hope someone can point me in the right direction? Below are an example of my tables, thanks in advance!
table1
------------
id (pk)
name
last_name
table 2
--------------
account id (pk)
id (fk)
image (holds image id from table 4)
table 3
-----------------
account id (fk)
image id (fk)
date
table 4
--------------------
image id (pk)
path
date
Try with this:
SELECT t1.*, t2.account_id, t4.path
FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table4 t4 ON t4.image_id = t2.image
So you have INNER JOINs between table1 & table2, and between table2 & table4. In MySQL, JOIN implicitly means INNER JOIN.
SELECT
table1.id,
table1.name,
table1.last_name,
table2.account_id,
table4.path
FROM
table1
JOIN table2 ON table1.id = table2.id
JOIN table4 ON table2.image = table4.image_id
WHERE table1.name LIKE '%tim%' AND table1.last_name LIKE '%smith%'
(Edit: Forgot to add in the original WHERE clause.)