I have this statement which gets an error because both phone and area_code are active record types with an id. Here I want to select the area_code id:
area_code_rec = #phone.area_codes.select(:id).where(:areacode => area_code).last
ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: Ambiguous column name 'id'
I believe you can specify which table the id is from like this:
area_code_rec = #phone.area_codes.select('phones.id').where(:areacode => area_code).last
Or use 'area_codes.id'.
It may be smart not to use a string name, rather directly the table name from your Model.
so, using some of Ruby's MetaProgramming magic:
area_code_rec = #phone.area_codes.select("#{AreaCode.table_name}.id").where(:areacode => area_code).last
area_code_rec = #phone.area_codes.select('area_codes.id').where(areacode: area_code).last
I am assuming the name of the area codes table is area_codes. If it is singular for some reason, it would be ...select('area_code.id')...
Related
in my table I have a column name "Status" and a RECORD type column name "Payment", which has another column name "Status" inside it. So I basically have 2 columns: "Status" and "Payment.Status". When I want to UNNEST the STRUCT data("Payment" column) and use the data, I will get the error message Column name STATUS is ambiguous
How do I solve this problem?
Thanks in advance
When you are trying to use 'STATUS', BigQuery does not know to which column you are referring to.
You can overcome this with giving your UNNEST() an alias and using it to reference the specific column you are using.
Example:
with example_data as (
select true as status,
[STRUCT( 123 as id, false as status)] as payment
)
select e.status,
p.id,
p.status as payment_status
from example_data e, unnest(payment) as p
So, I have a table table with three columns I am interested in : value, entity and field_entity.
There are other columns that are not important for this question. There are many different types of entity, and some of them can have the same field_entity, but those two columns determine what the column value refers to (if it is an id number for a person or the address or some other thing)
If I need the name of a person I would do something like this:
select value from table where entity = 'person' and field_entity = 'person_name';
My problem is I need to get a lot of different values (names, last names, address, documents, etc.), so the way I am doing it now is using a left join like this:
select
doc_type.value as doc_type,
doc.value as doc,
status.value as status
from
table doc
-- Get doc type
left join table doc_type
on doc.entity = doc_type.entity
and doc.transaction_id = doc_type.transaction_id
and doc_type.field_entity = 'document_type'
-- Get Status
left join table status
on doc.entity = status.entity
and doc.transaction_id = status.transaction_id
and status.field_entity = 'status'
where doc.entity = 'users' and doc.field_entity = 'document' and doc.transaction_id = 11111;
There are 16 values or more, and this can get a bit bulky and difficult to maintain, so I was wondering if some of you can point out a better way to do this?
Thanks!
I assume that you are not in position to alter the table structure, but can you add views to the database? If so, you can create views based on the different types of entities in your table.
For example:
CREATE VIEW view_person AS
SELECT value AS name
FROM doc
WHERE doc.entity = 'person'
AND doc.field_entity = 'name';
Then you can write clearer queries:
SELECT view_person.name FROM view_person;
I have a table called "people" with a column named "name". I would like to select all rows where the name is "bob" or "john". I have tried the following and many variants of it, none of which work. How can I do this correctly?
select * from people where name is bob or john;
Thanks
To compare a column with a value you need to use = not IS
select *
from people
where name = 'bob'
or name = 'john';
Alternatively you can use the IN operator.
select *
from people
where name IN ('bob','john');
Note that string comparison is case-sensitive in SQL. So the above will not return rows where the name is Bob or John
I want to know how I can take value associated to company_working_plan (name). I'm using MySqlCommand but I don't know if is possible get the specific column value without iterate on all rows.
NAME | VALUE
COMPANY_WORKING_PLAN XX
You can simply do it like this:
select value from tablename where name = 'company_working_plan'
try this:
SELECT name, value
FROM Tablename
WHERE ID = '13'
that is for the row you highlighted in your image if you want every row and only the value in the column as well as the company name try this:
SELECT name, value
FROM tablename
You edited the question after I posted this and it is still a bit confusing but I think you are looking for:
SELECT name, value
FROM tablename
WHERE value = 'company_working_plan'
I have a table called User. In the User table, there's a column called country. How do I write an SQL syntax to change all the user records whose country is "a" to country "b"?
This is a classic update statement:
UPDATE user
SET country = 'b' -- what to change
WHERE country = 'a' -- on which records
update "User" set "Country" = 'b' where "Country" = 'a'
PS: SQL is compatible with many backends. You didn't specify which one you have. That matters, because User maybe a keyword in some backends and/or casing might be significant.
UPDATE [User]
SET [country] = 'b' -- new value
WHERE [country]='a' -- previous value