ERR:Unknown column in where clause when query with struct - go-gorm

db.Table("test").Where(&Query{Name:"test"}).Find(&infos)
When I execute this statement, the SQL statement looks like this:
SELECT * FROM `test` WHERE (`queries`.`name` = 'test')
And the error:
Error 1054: Unknown column 'queries.name' in 'where clause'
It uses the name of my struct instead of the table name

Are you running the latest version of Gorm v2? I cannot reproduce what you see: the code you wrote produces
SELECT * FROM `test` WHERE `test`.`name` = "test"
In any case, if you don't use the Query struct, but instead write out the condition it should work:
db.Table("test").Where("name = ?", "test").Find(&infos)
Or you can use a map:
db.Table("test").Where(map[string]interface{}{"Name": "test"}).Find(&infos)

Related

select column in hibernate does not work but * does

I have this:
Query q = em.createNativeQuery("select * from TRANSAKTION",Transaktion.class);
List<Transaktion> trans = q.getResultList();
While this works, using a column name like id doesn't although it exists in database and generated entity class.
Other tables work fine it's just that one and with all columns. When I use * and .getId() it returns the correct id.
What could possibly be the problem?
The error message is:
SQL Exception: invalid column name

Postgresql 9.2 syntax issue

I am trying to execute this query using Postgresql 9.2
WITH udetail as (select(regexp_split_to_array('user#domain', E'\\#+')))
select * from udetail[1];
Buy it gives me a syntax error near where the start of '['. This same query is working fine under version 9.3. So I'm wonder if there's an alternative way of writing the query to get the same result.
I think you are looking for something like this:
WITH udetail(x) as (
select(regexp_split_to_array('user#domain', E'\\#+')))
select x[1] from udetail;
I don't think you can index a table expression like udetail in your case. It is the column of the table expression that is of array type. Hence, you can use an array subscript number on the column, not on the table itself.
Demo here
You should use an alias either as the query parameter:
with udetail(arr) as (
select regexp_split_to_array('user#domain', E'\\#+')
)
select arr[1] from udetail;
or as column alias:
with udetail as (
select regexp_split_to_array('user#domain', E'\\#+') as arr
)
select arr[1] from udetail;
You can also do it without aliases:
with udetail as (
select regexp_split_to_array('user#domain', E'\\#+')
)
select regexp_split_to_array[1] from udetail;

How to re-use a result set produced by executing a sql query?

I'm using PostgreSQL 8.4. I need to assign the result set produced by the query to a variable that I can use in another query. For instance, I have the following query:
SELECT *
FROM
-- some_table_expression
I need to use the result of that query in another query like the following:
FOR i IN 1 .. array_upper(players, 1)
LOOP
UPDATE partner.daily_profit
SET banner_id = (SELECT banner_id FROM _RESULT_OF_THE_QUERY_ WHERE id = players[i]),
WHERE partner.daily_profit.player_id = players[i]
END LOOP
Is there a way to do this in PostgreSQL 8.4? I would prefer to not insert the query string as subquery...

Regarding joins and subquery

I have below query that I am using ..
select * from app_subsys_param where assp_name like '%param_name%'
where param_name is the name of the parameter. From this query we will get the assp_id corresponding to the parameter. With this id we look up into app_subsys_parmval table to get the value of the parameter.
update app_subsys_parmval set aspv_value = 'true' where assp_id = id_val
Now instead of separately launching the two sql statements , I want to combime both of them as one is there any sub query or join mechanism that can combine both of them in one statement , please advise
You need to use UPDATE .. FROM syntax:
UPDATE app_subsys_paramval
SET aspv_value = 'true'
FROM app_subsys_param
WHERE app_subsys_param.id = app_subsys_paramval.id
AND app_subsys_param.value LIKE '%param_name%';
Use a subselect in your update statement:
UPDATE app_subsys_parmval
SET aspv_value = 'true'
WHERE id_val = (SELECT assp_id
FROM app_subsys_param
WHERE assp_name LIKE '%param_name%')
Note, I am assuming a bit about what's in the * of your select *.
Look at the MERGE statement. This is the ANSI SQL:2003 standard for UPDATE … FROM.
Documentation:
MERGE for DB2 for Linux/UNIX/Windows
MERGE for DB2 z/OS 9.1

mysql select all table names except

Alright, I know you can do the following to get all column names.
SHOW COLUMNS FROM person;
However, it does not allow where statements.
Is there any way to get the full list and just say not this one?
So I am basically looking for something like this:
select (show columns from person where Field <> 'id') from person
USe SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
Example:
SHOW CHARACTER SET WHERE `Default collation` LIKE '%japanese%';
More about show here
Try something like SHOW COLUMNS FROM person WHERE column != 'value';
Or you can use proper a SQL query against information_schema
select * from information_schema.columns
where table_schema = 'test' # your db name
and table_name = 'person '
and column_name != 'id'
For MySQL 4, you can use LIKE directly instead of WHERE http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
SHOW COLUMNS FROM 'Person' LIKE '%x%'
However unfortunately, that does not support a NOT clause.
If I may suggest
If you need this information from
MySQL console/phpAdmin/equivalent,
just eyeball it..
If you need this
from a front-end program like php, do
it from that environment.
MySQL documentation says that WHERE clause is allowed. Read the docs.
You cannot perform a true SHOW COLUMNS WHERE in MySQL 4. The docs show only an optional LIKE parameter for that version: http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
However, you can use the metadatabase information_schema.
USE information_schema;
SELECT * FROM `COLUMNS` WHERE `TABLE_NAME` = 'person' AND `COLUMN_NAME` != 'value';