Nested SELECT SQL Queries Workbench - mysql

Hi i have this query but its giving me an error of Operand should contain 1 column(s) not sure why?
Select *,
(Select *
FROM InstrumentModel
WHERE InstrumentModel.InstrumentModelID=Instrument.InstrumentModelID)
FROM Instrument

according to your query you wanted to get data from instrument and instrumentModel table and in your case its expecting "from table name " after your select * .when the subselect query runs to get its result its not finding table instrument.InstrumentModelId inorder to fetch result from both the table by matching you can use join .or you can also select perticuler fields by tableName.fieldName and in where condition use your condition.
like :
select Instrument.x,InstrumentModel.y
from instrument,instrumentModel
where instrument.x=instrumentModel.y

You can use a join to select from 2 connected tables
select *
from Instrument i
join InstrumentModel m on m.InstrumentModelID = i.InstrumentModelID

When you use subqueries in the column list, they need to return exactly one value. You can read more in the documentation
as a user commented in the documentation, using subqueries like this can ruin your performance:
when the same subquery is used several times, mysql does not use this fact to optimize the query, so be careful not to run into performance problems.
example:
SELECT
col0,
(SELECT col1 FROM table1 WHERE table1.id = table0.id),
(SELECT col2 FROM table1 WHERE table1.id = table0.id)
FROM
table0
WHERE ...
the join of table0 with table1 is executed once for EACH subquery, leading to very bad performance for this kind of query.
Therefore you should rather join the tables, as described by the other answer.

Related

Inline queries in MySQL 5.6

Could someone please show me how to use an inline query in MySQL 5.6
I am trying to do a simple thing! I am trying to write a query in which I select from a table in database and join with an inline table that I am trying to create on the fly.
It's a 1 column table and I am having trouble creating it with multiple rows.
Example inline query
select * from (
(select 'filename1' as file_name) as t1
union
(select 'filename2' as file_name) as t2
);
If I run this query without the second select statement, then it works.
But if I run it with both select statements and try to union them it breaks down and complains that I have an error in SQL syntax near "as t2".
Any help is greatly appreciated.
Obviously a really over-simplified example, but... The members of a union query do not take aliases. The derived table that your are creating with union does take one, though. So:
select * from (
select 'filename1' as file_name
union all
select 'filename2'
) t;
Note that:
there is no need to surround the union members with parentheses (unless you want individual order by clauses)
there is no need to alias the column(s) in the second member: the aliases defined in the first query prevail anyway
unless you do want to remove duplicates, use union all instead of union: it is more efficient, and makes the intent explicit
Finally: as your query stands, there is no need for the outer query. This is equivalent:
select 'filename1' as file_name
union all
select 'filename2'
Bonus: in very recent versions of MySQL, you can use the values row() constructor:
select *
from (values row('filename1'), row('filename2')) t(filename)

JOIN with a query that returns a query

Some SELECT statements stores in table as a field. I need to write SELECT statement that joins with some SELECT that returns SELECT.
For example:
SELECT *
FROM table1
JOIN (SELECT t_select FROM table2 WHERE = 'some_condition')
Last SELECT SELECT t_select FROM table2 returns some SELECT statement as text.
I need to join table1 with the result of the query that stores in t_select
Do I understand? Basically, you want to "evaluate" the SELECT that is stored in the table? That seems like a really poor design to me.
If you really need to do this, you'll need to pull the SELECT statement out yourself, and send it as a second query. You can't do this in pure MySQL.
Do you just want a subquery?
SELECT *
FROM table1 t1 JOIN
(SELECT t2.* FROM table2 t2 WHERE = 'some_condition') t2
on t1.<somecol> = t2.<someothercol>;
All in all you can't execute the query that is stored in the table withing another query. You will have to retrieve the query first, prepare it, and then execute it. Have a look at execute immediate :
http://dev.mysql.com/worklog/task/?id=2793
http://www.postgresql.org/docs/9.1/static/ecpg-sql-execute-immediate.html
Storing sql statements in a table is not very common, and there's usually better ways to do it.

Are there any differences in the following sql statements?

SELECT * FROM table t
SELECT t.* FROM table t
I tried it and it yielded the same results, but I want to make sure because I'm refactoring a piece of code that uses the second version, and I was surprised as it is both longer to write, and less simple.
Are there any hidden stuff here?
MySQL version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)
Both statements are the same in your case.
They would be not if you join multiple tables in one query.
select *
selects all columns.
select t.*
select all columns of table t (or the table assigned the alias t)
SELECT * FROM table t and SELECT t.* FROM table t
Return the whole table
SELECT t.* FROM table as t inner join table2 as t2
will only return the fields in the "table" table while
SELECT * FROM table as t inner join table2 as t2
will return the fields of table and table2
Both the statements will give same results until it's combined with another table with some table operator as Join, Apply where you will need to uniquely identify columns( more specifically ambiguous columns ) from this table.
As a best practice you should use column names instead of using select * as it makes code more readable and front end code doesn't break in case table structure gets changed at any point of time.
The statements are identical. All you have is an alias for table "table" called "t".
SELECT * will return all columns from all tables in the query. SELECT t.* will return all columns from the table named, or aliased as, t. The same in your example because there's only one table involved.

Join two MYSQL Query

I have two simple querys, but i need a little help to join them in one result
$query="SELECT * FROM USLUGE WHERE marka='1'";
And second query
$query2="SELECT * FROM USLUGE WHERE marka='2'";
I know the result from $query will give ALL from table USLUGE where marka=1, and result $query2 will give me ALL from table USLUGE wwhere marka=2
But i need to join this two querys in one query, and when i do a WHILE loop, to give me all result from both querys, not a two different WHILE loops, just one WHILE loops with results from both querys?
IS that possible, as example i gave a so simple query to understand :)
As long as the table structure is the same (more specifically, if both queries return the same number of fields and the same datatypes), you can use SQL UNION keyword, for example:
SELECT id, name FROM table1
UNION
SELECT id, name FROM table2
Note though that if the number of returned columns or datatypes do not match, then attempting to execute the query will result in an error, for example,
SELECT id, name, comment FROM table1
UNION
SELECT id, name FROM table2
will not work and will result in an error.
EDIT: with a rewritten question, using OR or IN in the WHERE clause is a much better solution:
SELECT * FROM USLUGE WHERE marka='1' OR marka='2'
or
SELECT * FROM USLUGE WHERE marka IN ('1', '2');
you can use union
SELECT * FROM USLUGE WHERE marka='1'
UNION
SELECT * FROM USLUGE WHERE marka='2'
however you should not use SELECT * type the column names instead
The more data is read from the tables, the slower the query will become. It increases the time it takes for the disk operations. Also when the database server is separate from the web server, you will have longer network delays due to the data having to be transferred between the servers.
It is a good habit to always specify which columns you need when you are doing your SELECT’s.
The question (as edited now) refers to only one table in which case
SELECT * FROM USLUGE WHERE marka='1' OR marka='2'
Would be better than doing a UNION

Mysql: How can I name a subselect in a join to avoid joining identical subselects?

I have a query like:
select * from (select ... ) t1 join (select ... ) t2 on t1._ = t2._
where the join subselects are identical. Is there an easy way to name this select so that I can use it both times? I tried this:
select * from (select ... ) t1 join t1 t2 on t1._ = t2._
but it gave an error. Any ideas?
If the cost of acquiring the rows in your subselect is significant, you may consider storing the intermediate result in a temporary table and then reference that twice in your select.
But you better measure this, because it also costs to store the intermediate result...
Can you share your query? Maybe you don't need to reference it twice after all?
CREATE VIEW MyCommonSelect (Col1, Col2. . .) AS
SELECT Col1, Col2. . .
Depending on exactly what your query looks like, you may be able to name the subqueries internally, but something like this tends to indicate that the subquery represents database logic that (in my opinion — others disagree) deserves its own name.