What is the equivalent to Access' 'SELECT DISTINCTROW * FROM...' in SQLite? - ms-access

I am converting a DB from Access to SQLite and therefore have to convert/debug all of the sql queries as well. Came across this one:
SELECT DISTINCTROW * FROM table WHERE column = value ORDER BY column2;
What is the equivalent query using SQLite?

SELECT DISTINCT * FROM table WHERE column = value ORDER BY column2;
Since there's only one table involved, DISTINCTROW acts like DISTINCT.

The equivalent is to ensure that all your tables have keys and that you implement joins and the rest of your query correctly. If you do that then you will never need anything like DISTINCTROW. DISTINCTROW is no more than just a legacy of silliness from Jet.
SELECT * is poor practice. List the columns by name.
SELECT column, column1, column2
FROM table
WHERE column = value
ORDER BY column2;

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)

Nested SELECT SQL Queries Workbench

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.

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.

Subquery for fetching table name

I have a query like this :
SELECT * FROM (SELECT linktable FROM adm_linkedfields WHERE name = 'company') as cbo WHERE group='BEST'
Basically, the table name for the main query is fetched through the subquery.
I get an error that #1054 - Unknown column 'group' in 'where clause'
When I investigate (removing the where clause), I find that the query only returns the subquery result at all times.
Subquery table adm_linkedfields has structure id | name | linktable
Currently am using MySQL with PDO but the query should be compatible with major DBs (viz. Oracle, MSSQL, PgSQL and MySQL)
Update:
The subquery should return the name of the table for the main query. In this case it will return tbl_company
The table tbl_company for the main query has this structure :
id | name | group
Thanks in advance.
Dynamic SQL doesn't work like that, what you created is an inline-view, read up on that. What's more, you can't create a dynamic sql query that will work on every db. If you have a limited number of linktables you could try using left-joins or unions to select from all tables but if you don't have a good reason you don't want that.
Just select the tablename in one query and then make another one to access the right table (by creating the query string in php).
Here is an issue:
SELECT * FROM (SELECT linktable FROM adm_linkedfields WHERE name = 'company') as cbo
WHERE group='BEST';
You are selecting from DT which contains only one column "linktable", then you cant put any other column in where clause of outer block. Think in terms of blocks the outer select is refering a DT which contains only one column.
Your problem is similar when you try to do:
create table t1(x1 int);
select * from t1 where z1 = 7; //error
Your query is:
SELECT *
FROM (SELECT linktable
FROM adm_linkedfields
WHERE name = 'company'
) cbo
WHERE group='BEST'
First, if you are interested in cross-database compatibility, do not name columns or tables after SQL reserved words. group is a really, really bad name for a column.
Second, the from clause is returning a table containing a list of names (of tables, but that is irrelevant). There is no column called group, so that is the problem you are having.
What can you do to fix this? A naive solution would be to run the subquery, run it, and use the resulting table name in a dynamic statement to execute the query you want.
The fundamental problem is your data structure. Having multiple tables with the same structure is generally a sign of a bad design. You basically have two choices.
One. If you have control over the database structure, put all the data in a single table, linktable for instance. This would have the information for all companies, and a column for group (or whatever you rename it). This solution is compatible across all databases. If you have lots and lots of data in the tables (think tens of millions of rows), then you might think about partitioning the data for performance reasons.
Two. If you don't have control over the data, create a view that concatenates all the tables together. Something like:
create view vw_linktable as
select 'table1' as which, t.* from table1 t union all
select 'table2', t.* from table2 t
This is also compatible across all databases.

(My)SQL: Wildcard in FROM clause?

I have a database with about 200 tables, and need to do a query on all tables containing a certain column (creation_date), but not all tables have that column. SELECT * FROM * WHERE creation_date>=42 obviously doesn't work, but what would be the best way of doing it?
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, TABLE_NAME,
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name ='creation_date']
copied directly from MySQL - you need to loop thru these system tables and the list will contain those ...
then you can build your SQL statements and be sure that they work ...
You could build a dynamic SQL query from metadata. I would approach this like:
Get a list of tables
For each table,
See if the table has the column creation_date
If it does, add your query on this table to the dynamic query
Union the results together
You might also be able to create a view on the multiple tables. Then you can just query the view.
You can try to include the table you want to query like so:
SELECT * FROM table1 a, table2 b WHERE a.creation_date>=42
You cannot use a wildcard in the from. In the way as shown above you can specify on which tables the where clause must apply. You can than leave out the ones that don't have the column.
So in the example query, table1 (alias a) has the column, and table2 (alias b) does not.