Executing SQL query on two tables

I am wondering if it is possible to make a sql query that does the same function as 'select products where barcode in table1 = barcode in table2

. I am writing this function in a python program. Once this function is called, will the table be joined permanently or only during the execution of this function? Thank you.

0


source to share


6 answers


SELECT t1.products
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t2.barcode = t1.barcode

      



+10


source


I think you want to join two tables:



http://www.w3schools.com/Sql/sql_join.asp

+7


source


Something like:

SELECT * FROM table1 WHERE barcode IN (SELECT barcode FROM table2)

      

Is this what you are looking for?

+1


source


SELECT table1.*, table2.* FROM table1 left join table2 on table1.barcode = table2.barcode

      

0


source


Here is an example of an inner join of two tables based on a common field in both tables.

SELECT table1.Products FROM table1 INNER JOIN table2 to table1.barcode = table2.barcode WHERE table1.Products is not null.

0


source


Here you can talk about yourself through the table design in these cases based on modeling the role of objects. (Yes, I understand that this is indirectly related to the question.)

You have products and barcodes. Products are uniquely identified by their product code (eg "A2111", barcodes are uniquely identified by value (eg 1002155061).

The product has a barcode. Questions: Can the product not have a barcode? Can the same product have multiple barcodes? Can multiple products have the same barcode? (If you have experience with UPC shortcuts, you know the answer to all of this is TRUE.)

So, you can make some statements:

The product (code) has zero or more barcode (value).
A barcode (value) has one or more products (code). - assumption: we, barcodes, do not have an independent existence if they were not / were / were not associated with products).

Which leads directly (via your ORM) to a two-table schema:


Product Code (PK) Description, etc.

ProductBarcode
ProductCode (FK) BarcodeValue
- with two-part natural primary key, ProductCode + BarcodeValue

and you link them together as described in other answers.

Similar statements can be used to determine which fields go into different tables in your design.

0


source







All Articles