Suppose I have two tables which are joined with each other,
I need to select data from them without using Join syntax. Is that practically possible?
If yes, Then How?
Thanks in Advance
vJ
Yes, it is. And it is perfectly valid.
SELECT tblOne.value1,tblTwo.value2 FROM tblOne,tblTwo WHERE tblOne.c1=tblTwo.c2
Use Unions.
Its syntax like-
SELECT a,b,c FROM table1
UNION
SELECT d,e,f FROM table2
UNION ALL will return all rows from both tables, if you only want DISTINCT rows then you will want to use UNION
Use two separate queries. Each query references one table.
The tables in the database aren't really "joined". There may be foreign key constraints defined, or the rows may be related by values stored in each table... but they aren't "joined".
A JOIN operation is the mechanism the relational database engine uses to match rows (using an algorithm, either nested loops, merge, or hash.)
The JOIN keyword is the ANSI standard for specifying that the database perform a join operation.
The old-school comma operator is an alternative to the JOIN keyword, but it's still a join operation.
Related
I have a SQL query of this format:
SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id;
I would like, for any given SQL SELECT query unknown in advance to know which tables were used to run it. So I thought I would use the EXPLAIN SELECT statement for that.
My issue is that the EXPLAIN SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id; query returns "t1" and "t2" as table names. I need it to give me the original table names, so table1 and table2 respectively. Now, I understand that it is not possible according to this old report.
However, I need to make this work somehow. I don't really want to run some REGEX on the query (unless you have one in mind that will undoubtedly include all scenarios of how tables can be used in a query, no matter how unstandardized it is).
I'm ready to hear all the possibilities that you might have in mind, it does not have to use the EXPLAIN SELECT as long as I can get all my original table names that were used in an unknown SELECT query. I don't care about the rest of the information provided by EXPLAIN SELECT, I just need the table names.
In case you want to propose a solution that is outside MySQL's scope, I am using PHP as the main platform to execute these requests with PDO (however, the queries are executed directly, they are not prepared statements).
What you're asking for can get really complex, because a table reference in SQL can be a view, or a common table expression, or a derived table subquery.
Any of those may be a JOIN of multiple tables, or a UNION/INTERSECT/EXCEPT of multiple queries.
It can even have no base table at all if it's a subquery that selects a tuple, or a VALUES statement.
I don't think there's a way to do what you want with regular expressions unless you're satisfied with a very reduced subset of SQL queries. You'd need a full-blown SQL parser to track the base table(s) per table reference.
First table:
Second table:
In the first Table, there will be multiple given_to with same taskid, and specific to that taskid i have set task in 2nd table.
Is it possible to obtain the task of a same taskid from multiple users to be printed in a table? If so How can we achieve it?
If possible, I also want to print the columns given_to of the task seperated by space.
Please Help
I'm not exactly sure I understood correctly what you want as a result, but as far as I get it you can achieve this with the correct SQL query, using a simple left join:
SELECT * FROM table1 LEFT JOIN table2 ON table1.taskid = table2.id
You might want to replace the SELECT * FROM ... part with the specific fields you are interested in.
For more information about joins (that is: merging results/columns from several tables into one query result) take a look at the MySQL reference manual on JOIN syntax.
Current query:
select * from `table1` where MATCH (`product_name`) AGAINST ('test' IN BOOLEAN MODE) AND `price` BETWEEN 0 AND 1000 order by MATCH (`product_name`) AGAINST ('test' IN BOOLEAN MODE) desc, `price` DESC LIMIT 0, 30
How can I run the above query on multiple tables with the same fields? eg table2, table3, table4
Would the query be faster if I were to combine all the data from all tables into 1 single table instead of multiple tables? Or would it make no difference at all?
Answer to 1st question : You can use JOINS in MySQL,that combines table as you say.
Answer to 2nd question :
One way :
SELECT
table1.this, table2.that, table2.somethingelse
FROM
table1, table2
WHERE
table1.foreignkey = table2.primarykey
AND (some other conditions)
Second way :
SELECT
table1.this, table2.that, table2.somethingelse
FROM
table1 INNER JOIN table2
ON table1.foreignkey = table2.primarykey
WHERE
(some other conditions)
The WHERE syntax is more relational model oriented where INNER JOIN is ANSI syntax which you should use.
The first query using WHERE become much much more confusing, hard to read, and hard to maintain once you need to start adding more tables to your query. Imagine doing that same query and type of join on four or five different tables ... it's a nightmare.
However, depending on the query optimizer, they may have the same meaning to the machine.
You should always code to be readable.
That is to say, if this is a built-in relationship, use the explicit join. if you are matching on weakly related data, use the where clause on the result set obtained from JOINS.
Hope this helps
How can we retrieving data from multiple tables without using "LEFT JOIN" or "RIGHT JOIN" in mysql?
OR
Is it Possible by using PHP/Mysql?
some of the case i can retrive data. such as when we use "two tables" or there have no complex "WHERE" condition.
But when i use 5,6,7----15 tables that case it is impossible for me to retrive data.
Please can any one help me.
Thanks Advance.
Do a search on the first table,
On the second table do a SELECT * FROM table_2 WHERE table_2.fk_table_1 IN ($pks_from_table_one)
and go on ... but this means you will do n queries, with n round trips to the DB - I've heard that some companies forbids JOINs and sometimes it is indeed better - but I didn't do it yet and don't recommend it either.
Let's say you have foods and people. You want to get the foods people like.
foods: id,food_name
people: id,name,food_id
food_id here refers to the id field in the foods table.
Simply do:
SELECT people.name,food.food_name FROM people,foods WHERE people.food_id=foods.id
You can use "alias" instead of "Join".
Assume you have 4 table.
Syntax:
Select * from Table1 A, Table2 B, Table3 C, Table4 D
where A.id= B.id and A.city=C.city and B.subject=D.subject
You can make this by adding a subquery without using any JOIN statement, in the example I gave you three tables were used to extract Users that are our customers and their mount is less or equal than 999, check it out..
Select TUsers.names TCustomers.names
FROM TUsers, TCustomers
Where TUsers.id = TCustomer.id AND TCustomers.id IN
(Select TCustomer.id
FROM TCustomer
WHERE TCustomer.amount >= 999)
Rate if it helps :)
I have six tables: t1, t2, t3, t4, t5, t6. And I also have one main table: main_table. TOTAL 7 TABLES!
Now, I am using SOLR for the searching of classifieds, and the results from solr are put into an array. The array results are ID:nrs which I use to match agains the same ID:s in MySql.
The first column of ALL tables in MySql is called classified_id.
If the user searches for "cars", then Solr will find all cars classifieds, put the id:s into an array and finally compare the MySql main table to match everything in table t1 (which is the cars table) where classified_id is the same in both tables.
The SOLR results array is first imploded, then:
SELECT * FROM classified, t1 WHERE classified.ad_id IN ('$solr_id_arr_imploded') AND classified.classified_id=t1.classified_id
My Q is, is this how I should do this? Is this how I should JOIN here, or use a LEFT JOIN? Is there any faster way of comparing to the array?
the table t1 may be empty if there are no car-classifieds...
Remember, the query could become very very long, for example if SOLR returns 10000 matches, then an array of 10000 id numbers which look like this would be returned: bmw_m3_low_miles_8948939
Thanks
An alternative to IN is to create a temporary table, fill it with the IDs, and perform an inner join on that. Make sure the other tables have an index on classified_id. To compare the options, use EXPLAIN.