I have a custom table with a file id that I want to join with the file_managed table.
My query looks like this:
SELECT ofertias_producto.sku AS sku
FROM
{ofertias_producto} ofertias_producto
INNER JOIN {file_managed} file_managed_ofertias_producto
ON ofertias_producto.id_imagen = file_managed_ofertias_producto.fid
LIMIT 10 OFFSET 0
The join is done right. The problem is that I'm not seeing any fields of the file managed table.
Could you tell me what I'm doing wrong? Thanks!
You did not SELECT any field from file_managed table
SELECT ofertias_producto.sku AS sku, file_managed_ofertias_producto.some_field as some_field
FROM
{ofertias_producto} ofertias_producto
INNER JOIN {file_managed} file_managed_ofertias_producto
ON ofertias_producto.id_imagen = file_managed_ofertias_producto.fid
LIMIT 10 OFFSET 0
Change some_field with appropriate field name of file managed table
Related
I have two tables. posts is the record I want, whilst post_tags allows a one-to-many relationship between them. NB. I have this working, however, they do not feel efficient to me and I could do with some SQLite gurus to help me out.
posts
id
post_tags
post_id
tag
Exact Match
I want to be able to retrieve posts that match a specific tag that I pass in.
Here is my current query.
SELECT * FROM posts WHERE "desiredTag" IN (SELECT tag FROM post_tags WHERE post_id = posts.id AND tag = "desiredTag");
One Of
My next requirement is an adaptation of the first. Instead of just matching one tag, I want to be able to pass in an array of tags and posts to be returned when they match at least one of the passed in tags. Again, I have this working but inefficiently.
SELECT * FROM posts WHERE ("tagOne" IN (SELECT tag FROM post_tags WHERE post_id = posts.id AND tag = "tagOne") OR "tagTwo" IN (SELECT tag FROM post_tags WHERE post_id = posts.id AND tag = "tagTwo"));
Usually EXISTS performs better:
SELECT p.*
FROM posts p
WHERE EXISTS (SELECT 1 FROM tag t WHERE t.post_id = p.id AND t.tag IN ('tag1', 'tag2'))
Or you could do it with an INNER JOIN:
SELECT DISTINCT p.*
FROM posts p INNER JOIN tag t
ON t.post_id = p.id
WHERE t.tag IN ('tag1', 'tag2')
Table A (goradid) has a field which is a FK to Table B(Spriden). I need two sets of data (spriden_id and Goradid_additional_id) from Table A and Table B and insert it into one of the field called e.g. bannerid and partyid. please see my code below as i am also getting a error SQL command not properly ended in PLSQL.
SELECT spriden_id, goradid_additional_id
FROM goradid, spriden
INNER JOIN spriden ON spriden_pidm = goradid_pidm
INTO bannerid, partyid;
The correct syntax is SELECT <COLUMN LIST> INTO <VARIABLE LIST>. So the query should be:
SELECT spriden_id, goradid_additional_id
INTO bannerid, partyid
FROM goradid a INNER JOIN spriden b ON b.spriden_pidm = a.goradid_pidm;
you can use aliasing for this.
it's simple.
SELECT spriden_id as bannerid, goradid_additional_id as partyid FROM goradid INNER JOIN spriden ON spriden_pidm = goradid_pidm;
Note: if you want to use into then you have to initialize the variable.
we use into usually.
declare
v_author author%rowtype;
begin
select * into v_author from author where author_key = 'A103';
dbms_output.put_line('Name:'||v_author.author_first_name||' '|| v_author.author_last_name);
end;
I have the following columns in a SQLite DB.
id,ts,origin,product,bid,ask,nextts
1,2016-10-18 20:20:54.733,SourceA,Dow,1.09812,1.0982,
2,2016-10-18 20:20:55.093,SourceB,Oil,7010.5,7011.5,
3,2016-10-18 20:20:55.149,SourceA,Dow,18159.0,18161.0,
How can I populate the 'next timestamp' column (nextts) with the next timestamp for the same product (ts), from the same source? I've been trying the following, but I can't seem to put a subquery in an UPDATE statement.
UPDATE TEST a SET nextts = (select ts
from TEST b
where b.id> a.id and a.origin = b.origin and a.product = b.product
order by id asc limit 1);
If I call this, I can display it, but I haven't found a way of updating the value yet.
select a.*,
(select ts
from TEST b
where b.id> a.id and a.origin = b.origin and a.product = b.product
order by id asc limit 1) as nextts
from TEST a
order by origin, a.id;
The problem is that you're using table alias for table in UPDATE statement, which is not allowed. You can skip alias from there and use unaliased (but table-name prefixed) reference to its columns (while keeping aliased references for the SELECT), like this:
UPDATE TEST
SET nextts = (
SELECT b.ts
FROM TEST b
WHERE b.id > TEST.id AND
TEST.origin = b.origin AND
TEST.product = b.product
ORDER BY b.id ASC
LIMIT 1
);
Prefixing unaliased column references with the table name is necessary for SQLite to identify that you're referencing to unaliased table. Otherwise the id column whould be understood as the id from the closest[*] possible data source, in which case it's the aliased table (as b alias), while we're interested in the unaliased table, therefore we need to explicitly tell SQLite that.
[*] Closest data source is the one listed in the same query, or parent query, or parent's parent query, etc. SQLite is looking for the first data source (going from inner part to the outside) in the query hierarchy that defines this column.
I'm writing stored procedures to get data based on a given id and that's all well and good, but some of the columns are foreign keys and what I need are their values. How do I do all of this within a stored procedure?
For reference, I will be using ASP.NET (haven't learned it yet) to call these stored procedures and update the website.
For instance, in the AdventureWorks database:
CREATE PROCEDURE spLoadProduct
AS
BEGIN
SET NOCOUNT ON;
SELECT
[ProductID], [Name],
[ProductCategoryID], [ProductModelID]
FROM
[SalesLT].[Product]
END
Screenshot of current SP result
What I want is for the last two columns to return the actual category and model names, not the foreign keys. Is the standard practice to do it all in one stored procedure as I am trying to do?
Maybe I am misinterpreting your question, but you can include joins in stored procedures.
Example if you have where afkey links out to table 2
Table_1: Table_2:
id name afkey akey otherinfo
1 fred 1 1 info
2 jon 2 2 other
3 abbie 3 3 more info
If you want to display the otherinfo column instead of the afkey foreign key
You could have:
create PROCEDURE spLoad
AS
BEGIN
SET NOCOUNT ON;
SELECT NAME, OTHERINFO
FROM TABLE_1
INNER JOIN TABLE_2
ON TABLE_1.afkey = TABLE_2.akey
END
You can use INNER JOIN to join the parent table to the child tables. In this case, the child tables are ProductCategory, and ProductModel.
CREATE PROCEDURE spLoadProduct
AS
BEGIN
SELECT
p.ProductID
, p.Name
, p.ProductCategoryID
, p.ProductModelID
, ProductCategoryName = pc.Name
, ProductModelName = pm.Name
FROM
SalesLT.Product p
INNER JOIN SalesLT.ProductCategory pc ON p.ProductCategoryID = pc.ProductCategoryID
INNER JOIN SalesLT.ProductModel pm ON p.ProductModelID = pm.ProductModelID;
END
Inner join tutorial: http://www.w3schools.com/sql/sql_join_inner.asp
I have a table (Threads) containing a field (id). I would like to select every row from Threads, as well as the number of rows in the table Posts where the field Posts.thread is the same as Threads.id.
How can this be done in SQL?
(Something like this pseudo-SQL: SELECT *, COUNT(* FROM Posts WHERE Posts.id=Threads.id) FROM Threads)
Sure - something like this?
SELECT
t.ThreadID,
(SELECT COUNT(*) FROM dbo.Posts p WHERE p.ThreadID = t.ThreadID)
FROM
dbo.Threads t
SELECT t.id, COUNT(p.thread)
FROM Threads AS t
LEFT OUTER JOIN Posts AS p
ON t.id = p.thread
GROUP BY t.id