MySQL select selected columns plus all other columns on command line - mysql

It used to be that I could fire a query like the following in a query window/sql command line, against MS SQL server:
SELECT foo1, foo2, * from bar
Basically show the specified columns followed by rest of the columns. But MySQL does not allow this; throws back a syntax error at me. Is there an alternative syntax to do this in MySQL? Note that I am NOT trying to do this in code (where it has no practical use); I am using it for firing random queries against my DB to look up information.

Just declare the table on the SELECT CLAUSE.
SELECT foo1, foo2, bar.* from bar;
OR
SELECT b.foo1, b.foo2, b.* from bar b;
;-)

If you name the table (either by using the full name, or by using an alias like below), you can actually get it to work (tested for version 5.5.31)
SELECT b.foo1, b.foo2, b.* FROM bar b

Related

how to include hard-coded value to output from mysql query?

I've created a MySQL sproc which returns 3 separate result sets. I'm implementing the npm mysql package downstream to exec the sproc and get a result structured in json with the 3 result sets. I need the ability to filter the json result sets that are returned based on some type of indicator in each result set. For example, if I wanted to get the result set from the json response which deals specifically with Suppliers then I could use some type of js filter similar to this:
var supplierResultSet = mySqlJsonResults.filter(x => x.ResultType === 'SupplierResults');
I think SQL Server provides the ability to include a hard-coded column value in a SQL result set like this:
select
'SupplierResults',
*
from
supplier
However, this approach appears to be invalid in MySQL b/c MySQL Workbench is telling me that the sproc syntax is invalid and won't let me save the changes. Do you know if something like what I'm trying to achieve is possible in MySQL and if not then can you recommend alternative approaches that would help me achieve my ultimate goal of including some type of fixed indicator in each result set to provide a handle for downstream filtering of the json response?
If I followed you correctly, you just need to prefix * with the table name or alias:
select 'SupplierResults' hardcoded, s.* from supplier s
As far as I know, this is the SQL Standard. select * is valid only when no other expression is added in the selec clause; SQL Server is lax about this, but most other databases follow the standard.
It is also a good idea to assign a name to the column that contains the hardcoded value (I named it hardcoded in the above query).
In MySQL you can simply put the * first:
SELECT *, 'SupplierResults'
FROM supplier
Demo on dbfiddle
To be more specific, in your case, in your query you would need to do this
select
'SupplierResults',
supplier.* -- <-- this
from
supplier
Try this
create table a (f1 int);
insert into a values (1);
select 'xxx', f1, a.* from a;
Basically, if there are other fields in select, prefix '*' with table name or alias

Select * from Select

Its very weird situation I know, nut I have got myself into it somehow. I have to connect to some other system service by passing some parameters in url.
In their service they are creating some query using parameter I pass.
For my case I have to pass 'Select' as a parameter name which is actually some class name on their side. So they end up in creating query as Select * from select
and some condition.
On execution I am getting error response as:
'There was a syntax error in a SQL query or filter expression at line
1, position 186. Saw \"Select\" but expected
'..SQL: \"SELECT col1, col2 FROM Select AS D where
some condition.
Can somebody help me on this.
Since Select is reserved word, you have to escape it by enclosing in backticks characters in order for MySQL to process your query:
select * from `select`
Its recommended not to use MySQL reserved keywords.. but if its necessary there is a solution..
Use this, it will work for you :
select * from yourdatabasename.select

AS clause in influx DB

How to use AS clause in influxDB?
SELECT os_family AS OsName, os_Image AS PlatformIcon FROM statistics
When I run this query I got following error.
ERROR: syntax error, unexpected AS, expecting FROM SELECT os_family AS OsName, os_Image AS PlatformIcon FROM statistics ^^
How to use SQL like AS clause in influx DB?
The AS clause in InfluxDB is meant to be used in two cases (described below). That said we can definitely add this as a new feature if this is a common use case. I don't know if that's necessary though, if you want to get back the columns as OsName then why did you name it os_family to begin with. We can discuss this further on the mailing list (you can find information about the mailing list among other means to reach us here http://influxdb.com/community/)
The two use cases are for joining multiple time series:
select * from foo as f inner join bar as b where f.somecolumn > 0`
and to alias aggregators:
select count(value) as c from foo where foo.value > 100

Select from View using Where clause to refer to another database - Unknown Column error

I am trying to select certain values from a view that I created. The statement is below:
SELECT * FROM dashboard.team
WHERE ac2012.acx_users.id = 1;
As you can see, there are 2 databases being referenced here:
dashboard database, team table
ac2012 database, acx_users.id table
ac2012.acx_users.id is the regular expression in the original Create View statement, I'm using that since of course I can't use an ALIAS in a Where clause... however, this is showing an error:
Error Code 1054: Unknown column 'ac2012.acx_users.id' in 'where clause'
I'm not sure how to get this to work, because I need to reference the other database in this case, but it's not recognizing the database. Any tips would be appreciated.
Since you're selecting from a view, the underlying databases aren't visible anymore. You only see what the view presents, as part of the database which the view lives in, so try WHERE acx_users.id = 1, or whatever you've aliased that field to in the view definition.
SELECT * FROM dashboard.team
LEFT OUTER JOIN ac2012 ON ac2012.CommonColumnName=dashboard.CommonColumnName
WHERE ac2012.acx_users.id = 1;
======================
Please replace by original column name ...

How to stop Access from "correcting" my queries?

When I have subqueries in Access, it tries to 'correct' them without asking and breaks the query in the process. For example,
Select * from TblA LEFT JOIN (Select * from [TblB] union Select * from [TblC]) as SubQry On TblA.Whatever = SubQry.Whatever
This works fine when I run it the first time. However, after I save it and try to reopen it, Access tries to be 'helpful' and changes the parens to brackets:
Select * from TblA LEFT JOIN [Select * from [TblB] union Select * from [TblC]]. as SubQry On TblA.Whatever = SubQry.Whatever
Of course, this throws an error. Is there an option somewhere in the options menu I can toggle to make Access stop trying to 'help' me?
(Footnote: The actual table names are more complex than "TblB" and need to be in brackets to be recognized).
AFAIK, you can't. Parentheses aroud your subqueries are always changed to square brackets, and you have to revert this if you want to re-save.
All you could do is save the sub query as a separate query and use that one until you application is really stable, and then integrate the sub query in the main one.