I have a table that contains a column user_ids which is a Postgres Array.
I need to select all messages from another table where the column user_id is one of the ids in the given array.
In Psuedo-sql:
select users.*
from users
where id IN a_postgres_array
Any ideas?
You could use the ANY operator. From your sample:
select users.*
from users
where id =ANY(a_postgres_array)
When using two tables, it could be a JOIN, something like:
SELECT users.*
FROM users INNER JOIN table_with_array ON users.id =ANY(table_with_array.a_postgres_array)
select users.*
from users
where id IN (
select unnest(a_postgres_array)
from t
where columnX = some_value
)
Related
Suppose, I have a table of contacts.
That table has contact IDs and Account IDs
So if I do
Select ContactID,AccountID From table
I get this:
Instead, I want to see this:
Is this plausible?
With two and only two contacts par account, aggregation seems like a straight-forward option:
select account_id, min(contactId) contactId1, max(contactId) contactId2
from mytable
group by account_id
If some accounts have 2 contacts and others have just 1, then:
select
account_id,
min(contactId) contactId1,
case when count(*) > 1 then max(contactId) end contactId2
from mytable
group by account_id
I have two tables. Table1 has company data (Company ID, Company Name ...), so single record for each company.
Table2 has information about departments in that company (Department ID, Department Name, Company ID, Company Name ... ). So, second table might have n number of records where same company id is used.
Problem is one of our trigger failed to work properly, and no one noticed till now. So, when Company Name was updated in Table1, it never reflected in Table2.
To correct this, I have to do something like the below query:
Update Table2
Set
[Company Name] = (select [Company Name]
from Table1
where Table2.Company ID = Table1.Company ID)
Group By Table2.Company ID
Basically, I am trying to update all records in Table2 to use the same name as Table1, for each record in Table1.
I am a bit confused about how to create the inner select clause.
P.S. Sorry, it might be a bit confusing. Kindly do let me know how to reword it the best.
Don't need to use group by ...
UPDATE T2
SET [Company Nane] = T1.[Company Nane]
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.[Company ID] = T2.[Company ID]
As other have mentioned: Remove GROUP BY and your query works fine.
GROUP BY is used to produce a result row in a query that is an aggregate of other rows. E.g. one record per company from your department table with the most busy department per company. You cannot update such a result record, for that record does not exist in the table. You can only update table records.
So remove GROUP BY from your query and you have it straight-forward.
Update DepartmentTable
Set [Company Name] =
(
select [Company Name]
from CompanyTable
where CompanyTable.[Company ID] = DepartmentTable.[Company ID]
);
I have about 100 select statements that look like this:
select * from users where clientid='GUID'
select * from providers where clientid='GUID'
They are really simple, but how I can I define the GUID once instead of having to put it into each select statement?
DECLARE #GuidVar VARCHAR(38);
SELECT #GuidVar ='GUID'
select * from users where clientid=#GuidVar
select * from providers where clientid=#GuidVar
use the IN clause and just put a list of comma separated IDs
SELECT * FROM users WHERE clientid IN (guid1,guid2,guid3)
MySQL IN Clause
Not sure if this might help you:
select * from users u
inner join providers p
on p.clientid = u.clientid
where u.clientid = 'GUID'
And you can continue to add inner joins for all tables and join on clientid. You will only require one where clause at the end.
The downside is that you will get all the columns in on row for each table. What you can do is to select specify tables or columns e.g select u.*, p.clientid
There are my tables:
http://i.imgur.com/dzwokhh.png
I want to write a query that return all info order by categoryId and Name.
For example: I want to return from right table id = 2,15,18 (CategoryId=1)
because in the left table they belong to Java (Id=1)
This should help to solve the problem:
select *
from mytable1
join mytable2 on mytable1.ID=mytable2.CategoryID
order by mytable1.ID ,Name
I am running the query :
select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
from
temp_table_excel_insert_for_join, city
where
temp_table_excel_insert_for_join.city = city.city
without problem now I want to have all these columns as a new table so I used
create table mytable as
( select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
from
temp_table_excel_insert_for_join, city
where
temp_table_excel_insert_for_join.city=city.city)
but it did not work for me what should I do to make it happen? I don't want to create a view I want to have a table.but I am not familiar if I should do a left join or other things
If your goal is to create a new table mytable with the columns person_id, city, city_id then use the select ... into syntax:
select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
into
mytable
from
temp_table_excel_insert_for_join
inner join
city on temp_table_excel_insert_for_join.city = city.city
Note that this will fail if you run it more than once as the table already would exist and you would have to drop it first.
See the documentation for more information on the into clause
Try this
select [temp_table_excel_insert_for_join].person_id
, [temp_table_excel_insert_for_join].city
,city.city_id
into mytable
from temp_table_excel_insert_for_join inner join city on temp_table_excel_insert_for_join.city=city.city)
Try separating the two operations. First:
CREATE TABLE MyTable ( etc)
Then
INSERT INTO MyTable ( <List of columns to insert> )
SELECT <Columns to insert>
FROM etc
WHERE etc
This works if you remove the brackets after your AS clause - at least in SQL Lite. So you can just write:
CREATE TABLE mytable AS
SELECT
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
FROM
temp_table_excel_insert_for_join, city
WHERE
temp_table_excel_insert_for_join.city=city.city;