I have two tables:
Table "items" that contains a list of items with a "rare" column.
Table "stash" that contains item_id and user_id.
I would like to get all items possessed by user_id 1 for example, and sort it by the item's "rare" (the rarest item will show first).
"rare" goes from 0 (not rare) to 3 (very rare).
How can I get the right query for that?
Thanks!
Something like this:
select s.*
from items i
join stash s on (i.id = s.item_id)
where s.user_id = 1
order by i.rare
Related
So I have a somewhat complicated mysql query question. I have 3 tables. One is a table of items. One is a table of categories. And one is a linking table that just has 2 fields, itemID and categoryID. It is a many to many relationship, so one item can be in multiple categories and each category can have multiple items. Now two of the fields in the category table are isactive and ismain. They are just bools of 1 or 0. I want to grab all items that only belong to categories where at either isactive=0 or ismain=0 or both.
I took some time and set up a sql fiddle for someone to play around with. http://sqlfiddle.com/#!9/b03842/2
Solution using subquery:
SELECT DISTINCT i.* FROM cart_item i
JOIN cart_item_category ic ON i.itemref = ic.itemref
WHERE ic.catid IN (
SELECT id FROM cart_category WHERE active = 0 OR ismain = 0
)
this is in mysql v. 4.x... i'm sure this is an rookie question but for some reason i can't get figure out the sql to get this to work. here is an approximation of the relevant tables:
Item
---
ItemID
etc.
ItemPerson
--------
PersonID
ItemID
TimeAccessed
Let's say I have Item ID's of 1,2,3,4,5
Let's say that I have 1 record in ItemPerson: {'JohnDoe',1,'12:00PM'}
I have a PersonName as input. I want returned a list of all items, including the time that item was accessed. If that item was not accessed by the given PersonName, I want NULL for the time.
I've tried the following:
SELECT i.*, ip.TimeAccessed
FROM Item i LEFT OUTER JOIN ItemPerson ip
ON i.ItemID = ip.ItemID
WHERE (ip.PersonName = 'JohnDoe' OR ip.PersonName IS NULL)
I get the expected results for 'JohnDoe'... All items are returned and ItemID 1 has a time.
If I change 'JohnDoe' to 'JaneDoe', I only get ItemID's 2-5 but I want my query to return all items, just with NULL for all the times.
LEFT JOINs can be tricky in these circumstances. You'll want to move the person condition into the ON clause; otherwise you are getting the items associated with that person or no one at all. (Logically speaking, optimizer aside, the WHERE occurs after the JOIN).
For an example lets say I have table INFO that contains columns:
ID - Name - Address
I also have a second table PURCHASES that contains columns:
Region - Name - Purchases
Multiple people can be in the same region, but each person only has one ID.
I want to write a query that will, based on a given ID in the INFO table, return all the rows in PURCHASES of people who live in the same region as the person with the specified ID.
I have done an Inner Join on Name for the two tables but can't figure out the best way to write a query.
Edit: My main problem is that there is no Region column in INFO. The only way to get the region is by joining to the PURCHASES table. Then I need the results of all rows containing that Region.
I am not sure if this is exactly what you want but you could probably twik it a bit to better fit your needs:
SELECT
Purchasse
FROM
PURCHASSE
INNER JOIN
INFO ON INFO.Name = PURCHASSE.Name
WHERE
INFO.ID = yourID
This should give you Purchasse for any given ID that the Name match for the two columns.
Try this:
Select * from PURCHASES LEFT OUTER JOIN INFO ON PURCHASES.NAME = INFO.NAME WHERE INFO.ID = givenID
Lets say I have a table with 2 columns, 1 contains Cars and 1 contains random numbers.
How can I search for the lastest "ferrari" entry in the table and know whats the random number for that ferrari?
I assume your Cars table has columns id, name,rnid
You can use following sql;
SELECT MAX(c.id) AS latestferrari FROM Cars c INNER JOIN randomnumbers rn ON c.rnid=rn.id WHERE c.name="ferrari"
I have to manage lists of items , so i built a table for the lists list_tb,one item_tb for the items (same item can be in more lists) and one table item_to_list_tb for the relation between item and lists
item_tb and item_to_list_tb they have in common item_id
list_id and item_to_list_tb they have in common list_id
each row of item_tb contains the name of the product so here i'm trying to get all the items that belong to a list $ID, and their name...i tried this but doesn't work well...
SELECT item_to_list_tb.*, item_tb.*
FROM item_to_list_tb
LEFT JOIN item_tb ON item_tb.item_id = item_tb.item_id
WHERE item_to_list_tb.list_id= $ID
GROUP BY item_tb.item_id
ORDER BY item_to_list_tb.item_ord
bottom line i need to retrieve a bunch of items aong with their name, stored in a different table...
whats the best practice? thanks!
Try something like:
SELECT item_tb.* FROM item_tb LEFT JOIN item_to_list_tb USING (item_id) WHERE item_to_list_db.list_id=$i