i have two tables : productprice and product
and i have a field name id on that tables, i would like update field name enddate on productprice base on code on product i try several syntax:
update productprice
set enddate = ’2016-12-31 00:00:00’
from product inner join
productprice
on product.id = productprice.id
where product.code = ‘9301940252’
but the result is "table name “productprice” specified more than once"
What I am doing wrong here? Thanks.
This is the correct syntax for MySQL:
update productprice pp join
product p
on p.id = pp.id
set pp.enddate = '2016-12-31'
where p.code = '9301940252';
Given your error and the fact that the question originally had Postgres and MySQL as a tag, perhaps you want Postgres syntax:
update productprice pp
set enddate = '2016-12-31'
from product p
where p.id = pp.id and p.code = '9301940252';
Related
I must create migration to update question image attribute but i need to check some attributes from other tables, my schema looks like this:
question {
id,
name,
groupId,
imageUrl -> i need to update this
}
group {
id,
name,
quizId
}
quiz {
id,
name,
type -> where type is 'History'
}
And i need to update attribute imageUrl from table question WHERE quiz type is for example 'History', i'm not sure how i can join this tables when using UPDATE. I tried something like this but it's not working like i want.
UPDATE question SET image_url = 'pathToImage' FROM quiz q WHERE q.type = 'History'
So this is best solution that i came up with, and it worked perfectly for my migration.
UPDATE question SET image_url = 'https://path_to_image.com' WHERE id IN (SELECT q.id FROM question q
JOIN group AS g ON q.groupi_id = g.id
JOIN quiz AS qu ON qu.id = g.quiz_id
WHERE qu.type = 'Lifestyle')
So basically i update image_url to all questions with ID's in sub query, and this is easiest way to do this.
try:
with cte as (
select q.id qid
from question q
join "group" g on g.id = groupId
join quiz z on z.id = quizId
where type = 'History'
)
UPDATE question q
SET image_url = 'pathToImage'
FROM cte.qid = q.id
JOIN three tables on this way:
UPDATE question q
SET image_url = 'pathToImage'
FROM (select g.Id
from group g
JOIN quiz z
ON g.quizID = z.Id
WHERE z.type = 'History') p
ON q.groupId = p.Id;
Hi is it possible to translate the below queries into linq ? ...
I am using entity frameworks core and tried to use the stored procedure but it seems like i have to create a model that applies to the metadata of the stored procedure. So i am trying to understand whether this kinda such query can be translated into linq so i don't have to create a separate db model.
SELECT
Stock.stockID ProductID,
stockName ProductName,
categoryName ProductCategory,
typeName ProductType,
sizeName ProductSize,
currentQuantity CurrentQuantity,
standardQuantity QuantityPerBox,
CONVERT(VARCHAR(255),CONVERT(INT,Stock.price)) AvgUnitCost,
CONVERT(VARCHAR(255),CONVERT(INT,x.lastUnitCost)) LastOrderUnitCost,
CONVERT(VARCHAR(10),CONVERT(DATE,x.lastOrderDate)) LastOrderDate
FROM dbo.Stock
LEFT JOIN
(
SELECT stockID,unitPrice lastUnitCost ,orderDate lastOrderDate, ROW_NUMBER() OVER (PARTITION BY stockID ORDER BY orderDate DESC) rn FROM dbo.SalesOrder
JOIN dbo.SalesOrderDetail ON SalesOrderDetail.salesOrderID = SalesOrder.salesOrderID
WHERE customerID = #customerID AND salesStatus = 'S'
) x ON x.stockID = Stock.stockID AND rn = 1
LEFT JOIN dbo.StockCategory ON StockCategory.stockCategoryID = Stock.stockCategoryID
LEFT JOIN dbo.StockType ON StockType.stockTypeID = Stock.stockTypeID
LEFT JOIN dbo.StockSize ON StockSize.stockSizeID = Stock.stockSizeID
WHERE disStock = 0
Almost everything is possible. you just need to be careful with performance.
var query =
from stock in db.Stocks
join x in (
from grp in (
from so in db.SalesOrders
join sod in db.SalesOrderDetails on so.SalesOrderId equals sod.SalesOrderId
where so.CustomerId == customerId && so.SalesStatus == "S"
orderby so.OrderDate descending
select new {
sod.StockId,
LastUnitCost = sod.UnitPrice,
LastOrderDate = so.OrderDate
} into inner
group inner by inner.StockId)
select grp.Take(1)) on x.StockId equals stock.StockId into lastStockSales
from x in lastStockSales.DefaultIfEmpty()
join sc in db.StockCategories on stock.StockCatergotyId equals sc.StockCategoryId into scLeft
from sc in scLeft.DefaultIfEmpty()
join st in db.StockTypes on stock.StockTypeId equals st.StockTypeId into stLeft
from st in stLeft.DefaultIfEmpty()
join ss in db.StockSizes on stock.StockSizeId equals ss.StockSizeId into ssLeft
from ss in ssLeft.DefaultIfEmpty()
where stock.DisStock == 0
select new MyDTO {
ProductId = stock.StockId,
ProductName = stock.StockName,
ProductType = st.TypeName,
ProductSize = ss.SizeName,
CurrentQuantity = stock.CurrentQuantity,
QuantityPerBox = stock.StandardQuantity,
AvgUnitCost = stock.Price,
LastOrderUnitCost = x.LastUnitCost,
LastOrderDate = x.LastOrderDate
};
As you can see is easy to rewrite these queries, I had to change a little bit the logic on how to get the latest sales for a stock item since ROW_NUMBER() OVER (PARTITION... is not supported from a LINQ perspective. Again, you would have to consider performance when rewriting queries.
Hope this helps.
I need your help. I need an advanced Query to my database. Im showing part of my database following:
Place (id, name, address)
Local (id, place_id, name)
PlaceReservation(id, local_id, date)
Media_Place (id, place_id, type)
Now I need a query, which gets all places with logo, which have AT LEAST ONE local which hasn't been reserved on a specific day e.g: 2015-07-01.
Help me please, because I haven't an idea how to do it. I thought about an outer join but I don't know how use it.
I was trying by:
$query = 'SELECT DISTINC *,
(SELECT sum(po.rating)/count(po.id)
FROM "Place_Opinion" po
WHERE po.place_id = p.id AND po.deleted = false) AS rating,
mp.path as logo_path
FROM "Place" p
INNER JOIN "Media_Place" mp ON mp.place_id = p.id
JOIN Local ON Local.place_id = Place.id
LEFT JOIN (
SELECT id AS rr, local_id
FROM PlaceReservation
WHERE date_start = \'2015-07-01\') Reserved ON Reserved.local_id = Local.id
WHERE mp.type = ' . Model_Row_MediaPlace::LOGO_TYPE . '
AND mp.deleted = false
AND p.deleted = false
AND rr IS NULL';
Looking for things that do not exist in a database is usually very inefficient. But you can change the logic around by finding places that do have a booking for the specified date, then LEFT JOIN that to all places with a logo and filter out the records with a reservation:
SELECT DISTINCT p.*, po.rating, mp.path as logo_path
FROM "Place" p
JOIN "Media_Place" mp ON mp.place_id = p.id AND mp.deleted = false AND mp.type = ?
JOIN Local ON Local.place_id = p.id
LEFT JOIN (
SELECT id AS rr, local_id
FROM PlaceReservation
WHERE date_start = '2015-07-01') reserved ON reserved.local_id = Local.id
LEFT JOIN (
SELECT place_id, avg(rating) AS rating
FROM "Place_Opinion"
WHERE deleted = false
GROUP BY place_id) po ON po.place_id = p.id
WHERE p.deleted = false
AND reserved.rr IS NULL;
The average rating per places is calculated in a separate sub-query. The error you had was because you referenced the "Place" table (p.id) before it was defined. For simple columns you can do that, but for sub-queries you can't.
I have this 3 table query that doesn't return any results. Does anybody have any suggestions on what I am doing wrong? _id is a global variable.
'If there is a guest list then display each person in it
Dim guests = (From g In myEntities.GuestLists
From p In myEntities.Pictures
From u In myEntities.UserProfiles
Where g.EventID = _id
Where p.UserID = g.GuestID
Where u.UserID = g.GuestID
Select New With {p.ImUrl, u.FirstName, u.LastName, u.UserID}).SingleOrDefault()
Repeater1.DataSource = guests
Repeater1.DataBind()
This syntax works for a single table or two table query so I assumed 3 tables would be the same.
I think you have to JOIN the tables.
Like this (untested!)
From g In myEntities.GuestLists
Join p In myEntities.Pictures On g.GuestID Equals p.UserID
Join u In myEntities.UserProfiles On g.GuestID Equals u.UserID
Where g.EventID == _id
Select New With {p.ImUrl, u.FirstName, u.LastName, u.UserID}
i have two tables.
BoardPosts
BoardPostId INT PK
ModifiedOn DATETIME NULLABLE
BoardComments
BoardCommentId INT PK
BoardPostId INT
CreatedOn DATETIME
A board post has zero to many comments.
I wish to set the ModifiedOn field to be the most recent comment date, if the board has a comment. Otherwise, just leave it null.
How do i do this, using TSql ?
something like ...
UPDATE BoardPosts
SET ModifiedOn = CreatedOn
SELECT TOP(1) CreatedOn
FROM BoardPosts a INNER JOIN BoardComments b ON a.BoardPostId = b.BoardPostId
???
I think this will work...
UPDATE BoardPosts
SET ModifiedOn = (SELECT MAX(CreatedOn)
FROM BoardComments
WHERE BoardComments.BoardPostId = BoardPosts.BoardPostId)
I decided to take into account both commented and uncommented posts:
update p1 set ModifiedOn = (
select
max(case when c.CreatedOn is null then p.CreatedOn
else c.CreatedOn end) as ModDate
from
boardposts p
left outer join BoardComments c on
p.BoardPostId = c.BoardPostId
where
p.BoardPostId = p1.BoardPostId
group by p.BoardPostId
)
from
BoardPosts p1
Hope this helps!