Update take 2
here is the two queries i'm working with (paging is omitted in both queries)
i'd like to get the following query
SELECT *
FROM product
LEFT OUTER JOIN
(
SELECT *
FROM Cart
LEFT OUTER JOIN
cartproducts
ON Cart.Id = cartproducts.Cart_id
WHERE Cart.username = 'user'
)
AS CartFiltered
ON product.Id = CartFiltered.product_id
but i always seem to get
SELECT *
FROM product
LEFT OUTER JOIN
cartproducts
ON product.Id = cartproducts.Product_id
LEFT OUTER JOIN
Cart
ON
cartproducts.cart_id = cart.id
WHERE Cart.username = 'user'
How can i manage to create the first type of query?
I hope my question is clearer :) lack of clarity is sometimes a great enemy of mine :p
Update:
FWIW, i still haven't found the answer, and am currently loading the paged product data and the whole cart to display the correct object.
Crude solution but it works and it beats the combinatorials i've been through trying to make the Criteria API recognize me as its master. I would be very interested if somebody could happen to point me in the right direction though ;)
Hello,
i'm having a hard time writing the following query in the Criteria API, and i don't really see how to do it: i hope some people can help.
On the database, i have products. Theses products can be in many carts (one cart per user), and each cart can contain many products, so we have a manytomany relationship.
I would like to display a list of every product, along with a small icon next to it to inform the user that this particular product is already in the cart. What i did is i asked NHibernate for my products, and to do a left outer join on carts filtered by the cart's owner.
Dim critPage As ICriteria = Session.CreateCriteria(GetType(Product)) _
.SetFirstResult(pageNumber * itemsPerPage).SetMaxResults(itemsPerPage) _
.CreateCriteria("Carts", "c", SqlCommand.JoinType.LeftOuterJoin) _
.SetProjection(plist) _
.SetResultTransformer(New TypedResultTransformer(Of ProductWithCartInfo)) _
.Add(Expression.Eq("c.User", username))
the projection list is here to reduce the number of columns to what's interesting for the ProductWithCartInfo class. It contains only property projections.
The problem is that with this query, the cart filtering is applied to the whole result set and i don't see every product with its presence in the user's cart, but rather every product in the user's cart.
Is it possible to do a left outer join on a subquery with the Criteria API in Nhibernate? For information, i would like to keep it in the Criteria API if possible.
Thanks
I'm not sure I entirly follow your issue but it sounds very similar to an issue I had. I seems that when you have the nested (sub) criteira with NHibernate you loose some of the control. I was able to get around my issue but aliasing my tables instead of using the nested criteira.
Possibly try...
criteria.CreateAlias("Cart", "Cart", JoinType.LeftOuterJoin);
and then your filter on the cart will have to be an OR condition
ICriterion cartCriterion = Restrictions.Eq("Cart.User", username);
customerCriterion = Restrictions.Or(customerCriterion, Restrictions.IsNull("Cart.User"));
criteria.Add(customerCriterion);
Let me know if that helps you out... if not... maybe post the SQL that your criteria above is generating and where it needs to change.
Good Luck
Related
So my basic goal is to create a database for a shopsystem, which is my task to do for my IT course. I tried to create a UPDATE-Query, that collects all the Sale Positions ("tblPosition.PositionAnzahl") ordered with a SELECT-Query and groups it by the products ordered, to have an overview about how often each product has been sold.
I want to do this to keep track of how many items are still left in the inventory.
The Query was supposed to update 1 field ("tblArtikel.ArtikelVerkauft") in my table "tblArtikel", in which all my articles and their information is stored.
However, i just found out that you cannot run UPDATE-Queries, that use SELECT-Query data, as i get a error, that says "Operation must use an updateable query".
This is the code i used for the query:
UPDATE tblArtikel as a JOIN
(SELECT p.PositionArtikelID, Sum(p.PositionAnzahl) AS SumOfPositionAnzahl
FROM tblPositionen as p
GROUP BY p.PositionArtikelID
) p
ON a.ArtikelID = p.PositionArtikelID
SET ArtikelVerkauft = p.SumOfPositionAnzahl;
Is there another way to keep track of all the Items left in my inventory, apart from doing what i did?
Here are screenshots of the 2 tables (the depending fields are circled red):
tblPositionen with field PositionAnzahl
tblArtikel with field ArtikelVerkauft
I have not worked with SQL before and only learned about it during 45 min, so ther emight be an easy way for this, but i would still appreciate every answer from you guys.
So I'm making a database of orders and I want to create a list of options for a dropdown box for an order form, but the list needs to be derived from a list of Services in another table, but it needs to match a preceeding Catagory Dropdown.
I'm not sure where I'm going wrong. The query is below.
SELECT ServiceTypes.ServiceType
FROM ServiceTypes
WHERE Orders.ServiceCatagory = ServiceTypes.ServiceType;
So orders is the db of Orders I want to pull the list into
ServiceTypes has the lists of of ServiceTypes I want to pull. It's composed of ServicType and ServiceCatagory. I need to limit the list of Service Types based on the Service Types in Orders.
So if someone selects the ServiceCatagory in orders to be "InstalL", the only results from ServiceTypes I want are those that have a ServiceCatagory that equals the Orders Service Catagory.
I suspect I need to join but I'm not sure how or what kind.
-update-
I should point out, I'm doing this in Access and just trying to populate a listbox.
My new query looks like this
SELECT ServiceTypes.ServiceType
FROM ServiceTypes
INNER JOIN Orders ON Orders.ServiceCatagory = ServiceTypes.ServiceCatagory;
Still not sure if that's right
So I've tested some of the SQL i've been using to get the information and I think I've actually got the correct code
SELECT ServiceTypes.ServiceType
FROM ServiceTypes
WHERE ServiceTypes.ServiceCatagory = Orders.ServiceCatagory
The real problem was my implementation in Access and where the query was being called.
I didn't need to use a JOIN, I just need to change where the call is being made. I.e. after the Catagory has been selected and not at the Database design level.
I'm trying to query for some stats on how many posts have been deleted as spam/abusive. I've got what I think should work, but the numbers it throws out don't make sense.
Since posts deleted this way are characterized by being both deleted and locked, I'm querying for those attributes by looking at the PostHistory table.
My initial query looks like this:
SELECT
COUNT(DISTINCT ph0.PostId)
FROM
PostHistory ph0
INNER JOIN
PostHistory ph1
ON
ph0.PostId = ph1.PostId AND
ph1.PostHistoryTypeId = 12
WHERE
ph0.PostHistoryTypeId = 14
That one throws up a count of 397, which doesn't make sense. There are at least 6485 posts that have been identified as spam on Stack Overflow. So, to check that query, I'm using a debugging query that outputs the post bodies:
SELECT
Body
FROM
PostsWithDeleted
WHERE
Id IN
(SELECT
DISTINCT ph0.PostId
FROM
PostHistory ph0
INNER JOIN
PostHistory ph1
ON
ph0.PostId = ph1.PostId AND
ph1.PostHistoryTypeId = 12
WHERE
ph0.PostHistoryTypeId = 14)
The bodies that outputs just plain aren't spam - certainly not of the kind I'm used to seeing. A sample:
All I can say is that you need to subclass UIView and make it a delegate of UIGestureRecognizerDelegate and UICollectionViewDelegate, then in your UIView subclass, do the following, I can't give out anymore information on this because the code, although owned by myself, is proprietary to the point of probably enraging quite a few organizations that I've used this for, so here's the secret...
I'm looking for an application or a social wall plugin to be added to a project. After looking at Wordpress and finally sifting through all the plugins (maybe all), I have come to a conclusion the plugins are not giving me enough customization options. For example, customizing the registration form. I need to add javascript for a combo box in order to display different options dependI added a movieclip here, and a number. To get an effect, like a star with a number...
The message is "starCount is not a child of a caller". I don't know...
So, what am I doing wrong that means I'm not selecting deleted, locked posts, and what do I need to do to fix it?
Although your query output is exactly the same as mine I believe my attempt gives a clear view of what you're trying to achieve.
If you find this not giving you the desired output, there must be some more logic into finding these posts that you mention.
Below query returns a number of posts that in their history have been marked as both Locked and Deleted at least once.
SELECT COUNT(*)
FROM (
SELECT
ph.PostId
FROM
PostHistory ph
INNER JOIN PostHistoryTypes pht ON
ph.PostHistoryTypeId = pht.id
WHERE
pht.Name IN ('Post Locked', 'Post Deleted')
GROUP BY ph.PostId
HAVING COUNT(DISTINCT ph.PostHistoryTypeId) >= 2
) foo
You've included below message and it seems alright with your query.
Since posts deleted this way are characterized by being both deleted and locked, I'm querying for those attributes by looking at the PostHistory table.
Every article I need to solve my problem seems to be in C# and I need a solution in VB.NET.
I'm using EF 6.0 with Database First model. Let me use the classic Customer product scenario to demonstrate my situation. In my database I have three tables Customer, Product and CustomerProduct. See this example in this link as mine is exactly the same.
After I generate my model from the database, my entity model diagram shows that the CustomerProduct has disappeared as expected and the the model shows a many to many relationship between Customer and Product also as expected with navigational properties of Products in Customer and Customers in Product.
All I want to do is find the product related to a customer pull out some data from both tables namely CustName and ProductName.
The SQL I would use is:
SELECT c.CustName, p.ProductName FROM Customer c
INNER JOIN CustomerProduct cp on c.CustomerId = cp.CustomerId
INNER JOIN Product p on cp.ProductId = p.ProductId
WHERE c.CustomerId=101
I don't know how to use the Addresses navigational property to access the Address data in one query.
You include them and then access them via the property in the Entity class.
Dim query = model.User.Include("Address").Include("UserAddressLink").Where(Function(o) o.UserId = 101).FirstOrDefault
If Not query Is Nothing Then
Dim houseNumber = query.Address.HouseNo 'uses the navigation property
End If
Thanks to InteXX I managed to work it out. This is my whole solution
Using db as new CustProdEntities
Dim query = db.Customers.Include(Function(U) U.Products).ToList
txtCustomer.Text = query.First.CustName
txtProduct.Text query.First.Products.First.ProdName
End Using
The bit I was stuck on was having to filter twice to the Product data. I'm not sure if there's an easier way to do this but it works for now.
My models have a many-to-many relationship where Coach could have coached many Teams, and a Team could have multiple coaches.(Assistant, Head, etc)
in Rails Console, when I run:
#coach = Coach.joins(:teams).select("coaches.first_name, coaches.last_name, teams.team_level")
returns:
=> [#<Coach first_name: "john", last_name: "doe">]
notice that it doesnt return the teams.team_level, so I cant use #coach.team_level on my view
When I do .to_sql it returns:
=> "SELECT coaches.first_name, coaches.last_name, teams.team_level
FROM `coaches`
INNER JOIN `coach_teams` ON `coach_teams`.`coach_id` = `coaches`.`id`
INNER JOIN `teams` ON `teams`.`id` = `coach_teams`.`team_id`
Which is what I expect... So when I run this Query against my DB, I get the expected fields.
What am I doing wrong here/what am I not seeing? Thanks for looking into this!
You're not doing anything wrong, you use a method of the Coach model so you get Coach model/s.
Since you're using a join on teams you can access the team_level value without an additional query.
It is actually good. You ask for a Coach model, you join the Team model with it.
So you actually just need #coach.teams.team_level to access the team level.
The select can't change the schema of your modell, it can only filter down the attributes returned form the SQL, this way you need less roundtrip and less data transfer, but the structure is unchanged.