I am trying to select two tables with where clause, Show The problem: I am getting more than 2 result. something like The tables don't have the same structure and are not related by any means. Any ideas?
asked Oct 29, 2013 at 4:04
6 You need to have a join between table1 and table2 on some unique column, say id.
Additionally you can have multiple filter conditions( say you want to filter the tables on id=101 -
Hope this helps. Whenever you have multiple tables in a SQL statement, you need to join them otherwise the engine would make cartesian product as it happens in Cartesian product of mathematical set theory. Basically you should have at least n-1 join conditions where n is the number of tables used. answered Oct 29, 2013 at 4:09
Aditya KakirdeAditya Kakirde 4,5651 gold badge12 silver badges10 bronze badges 2 Your question is a little problematic, but if your problem is not getting two id's, but you are getting one correctly with the use of a JOIN, you may be looking for a IN clause:
Using IN instead of = lets you match multiple values to the table.id. This way, you get data from both tables and you get both ID's answered Oct 30, 2013 at 1:05
Filipe SilvaFilipe Silva 20.8k5 gold badges50 silver badges67 bronze badges This is join usage :
answered Oct 29, 2013 at 4:14
Hi friends, In this tutorial we will see how to join more than one tables in PHP & MySQL using SQL join statement and without using join statement, though this is very common requirement when it comes to database and we need sometimes to fetch data from more than one table as per tables relation so in that case we can write up join query to get data and by joining them it can be showed in proper format. so here in this tutorial i have taken two tables one is category and another is product so that you can understand easily. before proceeding you can check out the demo so that you can get idea about joining tables, so let's get started. Consider Following Two tables.1 : tbl_categories
Records for this table :
2 : tbl_products
Records for this table :
- In above two tables there exist one common column(field) named 'cat_id' based on which you can establish relatinship between these two tables. 1. Single SQL statementIn order to retrieve information from from two related tables you need to reference two tables in your SQL query. Without Join general syntax:
- in the above syntax WHERE condition establish relation between tables based on the common field. Example.index.php
In above example Query used as follow :
Query
explained : Output : 2. using JOIN-In MySql JOINs allows the retrieval of data records from one or more tables having same relation between them, you can also use logical
operator to get the desired output from MySql join queries.
2. Outer JOIN Syntax :
that's it... How can I get data from two tables in PHP?The LEFT JOIN keyword is used to return all records from the left table (table1), and the matching records from the right table (table2). Syntax : SELECT column1,column2,... columnn FROM table1 LEFT JOIN table2 ON table1.
How can I get data from two tables?In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.
How do you SELECT two tables in a single query?To retrieve information from more than one table, you need to join those tables together. This can be done using JOIN methods, or you can use a second SELECT statement inside your main SELECT query—a subquery.
How do I join two tables in a query?You create an inner join by dragging a field from one data source to a field on another data source. Access displays a line between the two fields to show that a join has been created. The names of the tables from which records are combined.
|