SELECT where ID matches one of a list - mysql

I'm just learning the ins and outs of MYSQL queries but I've run into a roadblock with a project and I'd appreciate some help reaching a solution.
I have two tables, the first contains a reference to the entry (entry_id) and the modification associated (mod_id) as well as the category of modification it refers to (mod_name)
Table 1: exp_store_product_options
mod_id entry_id mod_name
3 2919 COLOR
4 2311 SIZE
5 2311 COLOR
6 3301 COLOR
the second table contains two relevant fields, mod_id and opt_name
Table 2: exp_store_product_modifiers
mod_id opt_name
3 BLACK
4 EU 44
5 BROWN
6 BROWN
What I am trying to achieve, is a listing of all the DISTINCT opt_name fields that (through a join on the mod_id) correspond to entry_ids that I would like to pass in as a lump.
here is the code I've come up with so far, I believe it'll do what I need aside from requiring me to loop through the query for each entry id, and failing on the DISTINCT requirement since for each iteration, everything is distinct. (the {sale_products} tags are from ExpressionEngine, and will loop during the parse to provide me with a list of the entry_id results that are relevant to this page
SELECT DISTINCT opt_name
FROM exp_store_product_options
INNER JOIN exp_store_product_modifiers
ON exp_store_product_options.product_mod_id=exp_store_product_modifiers.product_mod_id
{sale_products parse='inward'}entry_id = '{entry_id}' OR{/sale_products}
AND mod_name = 'SIZE'
====================================================
POSTMORTEM
Just in case anyone else is trying to work within expresso's Store module for ExpressionEngine and needs to build some rudimentary filtering into your templates, here's the code I ultimately got to work. Very similar to Ben's code, but utilizing embeds instead of directly inserting the entry_ids because of issues with parse order:
the template: embeds/product_filter
{exp:query sql="SELECT DISTINCT opt_name
FROM exp_store_product_modifiers
LEFT JOIN exp_store_product_options
ON exp_store_product_options.product_mod_id = exp_store_product_modifiers.product_mod_id
WHERE exp_store_product_modifiers.entry_id IN ({embed:entry_ids})
AND exp_store_product_modifiers.mod_name = '{embed:filter_on}'"
}
<li>{opt_name}</li>
{/exp:query}
with an {embed} that looks like
{embed="embeds/product_filter" entry_ids="{sale_products backspace='1'}{entry_id},{/sale_products}" filter_on="SIZE"}

If you have a list of entries, you can use IN. Also, I'd use a LEFT JOIN here instead of an INNER JOIN.
SELECT DISTINCT opt_name
FROM exp_store_product_options
LEFT JOIN exp_store_product_modifiers
ON exp_store_product_options.product_mod_id = exp_store_product_modifiers.product_mod_id
WHERE exp_store_product_options.entry_id IN (1,2,3)
AND mod_name = 'SIZE'

Related

SQL Query - Distinct on One Column for Distinct Value of Other (with INNER JOIN)

I appreciate that questions similar to this one have been asked on here before but I have thus far been unable to implement the answers provided into my code both because of wanting to distinguish duplicates in one column only whilst the other stays the same and the INNER JOIN in my code. The INNER JOIN is problematic because most of the provided answers use the PARTITION function and, being a novice with SQL, I do not know how to integrate this with it. Advice just on using INNER JOIN with PARTITION would be useful.
Whilst I could do this post-export in Python (where I will be using the desired output), this code currently outputs ~2 million rows, making it time-consuming to work with and check. Here is the code:
SELECT client_ip_address, language_enum_code
FROM vw_user_session_log AS usl
INNER JOIN vw_user_topic_ownership AS uto
ON usl.user_id = uto.user_id
Using SELECT DISTINCT instead of SELECT gets me closer to the desired output but rather than leaving one duplicate row behind it removes all of them. Advice on using this function whilst preserving one of the duplicate rows would be preferred. I am on a read-only connection to the database so the DELETE FROM approach seen here would only be viable if I could make a temporary query-able table from the query output which I don't think is possible and seems clumsy.
Raw data sample:
user_id: client_ip_address: language_enum_code: (other stuff...)
4 194:4:62:18 107
2 101:9:23:34 14
3 180:4:87:99 15
3 194:4:62:18 15
4 166:1:19:27 107
2 166:1:19:27 14
Desired result:
user_id: client_ip_address: language_enum_code: (other stuff...)
4 194:4:62:18 107
2 101:9:23:34 14
3 180:4:87:99 15
As you can see, any id-enum combination should be filtered to occur only once. The reason this is not any ip-enum combination is that multiple users can connect through the same IP address.
If you don't care which IP address you keep for each user_id / enum combo, then something like this should do:
SELECT user_id, min(client_ip_address), language_enum_code
FROM vw_user_session_log AS usl
INNER JOIN vw_user_topic_ownership AS uto
ON usl.user_id = uto.user_id
where client_ip_address is not null
group by user_id, language_enum_code
Do you simply want aggregation?
SELECT client_ip_address, GROUP_CONCAT(DISTINCT language_enum_code)
FROM vw_user_session_log usl INNER JOIN
vw_user_topic_ownership uto
ON usl.user_id = uto.user_id
GROUP BY client_ip_address;
This will return one row per client_ip_address with each language code in a comma delimited list.
You can also use MIN() or MAX() to get an arbitrary value for language_enum_code for each client_ip_address.

MySQL query for multiple table data

I'm searching for a solution to a problem within MySQL which doesn't sound too complicated, I thought.
Basically I want to use two tables.
The first does contain an electronic component list like
ID Description Value AdditonalInfo
1 Resistor 1.0R R0402
2 Capacitor 100nF C0805
3 Capacitor 10nF C0603
...
I want to store information about the sourcing within a second table.
ID Component Manufacturer Partnumber Timestamp
1 2 TDK XXXYYYZZZ 5
2 2 Kemet AAABBBCCC 10
3 1 Multicomp 111222333 3
...
As you can see, it should be possible to add more than one manufacturer for each component.
Now, I want to generate a single table (a view) which should contain
all component information AND if present, the latest entry of the manufacturer.
For the given example that would be
ID Description Value AdditonalInfo Manufacturer Partnumber
1 Resistor 1.0R R0402 Multicomp 111222333
2 Capacitor 100nF C0805 Kemet AAABBBCCC
3 Capacitor 10nF C0603 (NULL)
Would this be possible within a single query? Or at least with some kind of query which
generates the final table? I could not find out, if the JOIN command would do that.
I would appreciate any help or hints to find a solution for this.
Thanks!
The following query should give you what you are after.
It takes all the components, and then for each component shows the matching entries in the sub-query against the sourcing table which groups the components by the latest entry.
The sub-query is joined based on the component and max(timestamp) to another copy of the sourcing table to get the remaining information required.
SELECT a.ID, a.Description, a.Value, a.AdditonalInfo,
c.Manufacturer, c.Partnumber
FROM componentTable a
LEFT JOIN ( SELECT component, max(timestamp) AS maxTime
FROM sourcingTable
GROUP BY component
) b
ON a.id = b.component
INNER JOIN sourcingTable c
ON b.component = c.component
AND b.maxTime = c.timestamp
You may need additional bracketing around the LEFT JOIN and INNER JOIN parts, but give this a try first and let me know if it doesn't work
Have your try this:
SELECT * FROM table1 LEFT JOIN table2 on table1.id = table2.id
You can get data using query with JOIN, if you have index on Component column:
SELECT
*
FROM components
INNER JOIN store USING(id)
This query will enough and you don't need store redundant data.

MySQL query conditional or subquery

I'm finding that SQL related questions are very difficult to express conversationally so please forgive me if this makes no sense.
I'm migrating data out of a CMS database to MySQL and I would like to simplify the data structure wherever possible. The current category scheme has 7 categories and has an item to category relationship setup as a many-to-many so a junction table is required.
It would be better setup as two one-to-many relationships so that an extra table is not required. The actual category setup that I need is as follows:
Category 1
Option 1
Option 2
Category 2
Option 3
Option 4
Option 5
Option 6
Option 7
Each item belongs to one value for Category 1, and one value for Category 2. This is the general form of the query I'm creating:
SELECT items.entry_id, categories.cat_id
FROM items
INNER JOIN categories
ON items.entry_id = categories.entry_id
WHERE items.item_type = 6
How would I add a conditional or sub query so that I got the results of the SELECT clause like:
SELECT items.entry_id, categories.cat_id1, categories.cat_id2
where the value of cat_id1 and cat_id2 are the values described above?
****Update****
I have made some progress getting the query I need (the tables are too complicated to post here for examples but here is a sample query):
SELECT exp_weblog_data.entry_id,
exp_weblog_data.field_id_27 AS Title,
exp_weblog_data.field_id_29,
exp_weblog_data.field_id_32,
exp_weblog_data.field_id_33,
exp_weblog_data.field_id_28,
exp_weblog_data.field_id_84,
exp_relationships.rel_child_id,
CASE WHEN exp_category_posts.cat_id = '15' OR exp_category_posts.cat_id = '16' THEN exp_category_posts.cat_id END as cat1,
CASE WHEN exp_category_posts.cat_id = '17' OR exp_category_posts.cat_id = '20' THEN exp_category_posts.cat_id END as cat2
FROM exp_weblog_data
INNER JOIN exp_relationships
ON exp_weblog_data.entry_id = exp_relationships.rel_parent_id
INNER JOIN exp_category_posts
ON exp_weblog_data.entry_id = exp_category_posts.entry_id
WHERE exp_weblog_data.weblog_id = 6
This gets me the two columns I want for Cat1 and Cat 2 but there are still two problems here - the inner join on exp_category_posts is resulting in 2 rows for each record where I only want one row for each value of entry_id. Secondly, in the case statements, I want to set a value of A if cat_id = 15 and B if cat_id = 16 but I can't seem to find the right syntax for this without getting errors.
I hope this clears things up a bit!
There is no reasonable way to avoid an extra table when setting up a many-to-many relationship.
Do not have multiple columns for multiple categories (cat_id1, cat_id2, ...).
Do not put multiple ids in a single column ("123,234,345").
Both of those lead to problems that are worse than having the mapping table.

SELECT, JOIN, and a WHERE != statement returning too many rows

Trying to get a tricky mysql select statement working - and I need some help to cool off my burning noggin...and I have a feeling one of you MYSQL heroes out there will look at this and reel it off.
Goal: List a given user's songs NOT in a given category.
Three tables: :
table 1: song, many fields with assigned UserID, and unique SongID
table 2: category, 3+ fields, with assigned UserID and unique CatID
table 3: linker, one-to-many for listing songs in one or more categories. 3 fields, unique id (index), SongID, CatID
The following gets me close - but does not list a user's songs that aren't assigned to any other category at all OR are a already assigned to a another category (I think thanks to !=).
SELECT DISTINCT song.SongName, song.UserID, song.SongID FROM song
JOIN linker ON song.SongID = linker.SongID
WHERE linker.CatID != 155 AND song.UserID =2
Then, I tried this,
SELECT DISTINCT song.SongName, song.UserID, song.SongID FROM song
LEFT JOIN linker ON song.SongID = linker.SongID
WHERE (linker.SongID IS NULL OR linker.CatID != 155) AND song.UserID =2
Closer but not working (still thanks to != including songs already assigned).
I was thinking I can get away without invoking table 2, since it merely adds and defines categories for a given user. Alternatively, I'm thinking of getting all the user's songs, and then unsetting array values with a given CatID - but this doesn't seem like it should be necessary? I feel like I'm missing something simple? Table structure is not sacred at this point if it absolutely needs to change. Thanks to any who share their thoughts.
Try this (I am used to MSSQL so if my syntax is off, appologies in advance):
SELECT s.SongName, s.UserID, s.SongID
FROM song s
LEFT JOIN linker l on s.SongID = l.SongID AND l.CatID = 155
WHERE s.UserID = 2
AND l.ID is null
SELECT DISTINCT song.SongName, song.UserID, song.SongID
FROM song
LEFT JOIN linker
ON song.SongID = linker.SongID
and linker.CatID != 155
AND song.UserID = 2

Using Joins, Group By and Sub Queries, Oh My!

I have a database with a table for details of ponies, another for details of contacts (owners and breeders), and then several other small tables for parameters (colours, counties, area codes, etc.). To give me a list of existing pony profiles, with their various details given, i use the following query:
SELECT *
FROM profiles
INNER JOIN prm_breedgender
ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN contacts
ON profiles.ProfileOwnerID = contacts.ContactID
INNER JOIN prm_breedcolour
ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
ORDER BY profiles.ProfileYearOfBirth ASC $limit
In the above sample, the 'profiles' table is my primary table (holding the Ponies info), 'contacts' is second in importance holding as it does the owner and breeder info. The lesser parameter tables can be identified by their prm_ prefix. The above query works fine, but i want to do more.
The first big issue is that I wish to GROUP the results by gender: Stallions, Mares, Geldings... I used << GROUP BY prm_breedgender.BreedGender >> or << GROUP BY ProfileBreedGenderID >> before my ORDER BY line, but than only returns two results from all my available profiles. I have read up on this, and apparantly need to reorganise my query to accomodate GROUP within my primary SELECT clause. How to do this however, gets me verrrrrrry confused. Step by step help here would be fantabulous.
As a further note on the above - You may have noticed the $limit var at the end of my query. This is for pagination, a feature I want to keep. I shouldn't think that's an issue however.
My secondary issue is more of an organisational one. You can see where I have pulled my Owner information from the contacts table here:
LEFT JOIN contacts
ON profiles.ProfileOwnerID = contacts.ContactID
I could add another stipulation:
AND profiles.ProfileBreederID = contacts.ContactID
with the intention of being able to list a pony's Owner and Breeder, where info on either is available. I'm not sure how to echo out this info though, as $row['ContactName'] could apply in either the capacity of owner OR breeder.
Is this a case of simply running two queries rather than one? Assigning a variable $foo to the first run of the query, then just run another separate query altogether and assign $bar to those results? Or is there a smarter way of doing it all in the one query (e.g. $row['ContactName']First-iteration, $row['ContactName']Second-iteration)? Advice here would be much appreciated.
And That's it! I've tried to be as clear as possible, and do really appreciate any help or advice at all you can give. Thanks in advance.
##########################################################################EDIT
My query currently stands as an amalgam of that provided by Cularis and Symcbean:
SELECT *
FROM (
profiles
INNER JOIN prm_breedgender
ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN contacts AS owners
ON profiles.ProfileOwnerID = owners.ContactID
INNER JOIN prm_breedcolour
ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
)
LEFT JOIN contacts AS breeders
ON profiles.ProfileBreederID = breeders.ContactID
ORDER BY prm_breedgender.BreedGender ASC, profiles.ProfileYearOfBirth ASC $limit
It works insofar as the results are being arranged as I had hoped: i.e. by age and gender. However, I cannot seem to get the alias' to work in relation to the contacts queries (breeder and owner). No error is displayed, and neither are any Owners or Breeders. Any further clarification on this would be hugely appreciated.
P.s. I dropped the alias given to the final LEFT JOIN by Symcbean's example, as I could not get the resulting ORDER BY statement to work for me - my own fault, I'm certain. Nonetheless, it works now although this may be what is causing the issue with the contacts query.
GROUP in SQL terms means using aggregate functions over a group of entries. I guess what you want is order by gender:
ORDER BY prm_breedgender.BreedGender ASC, profiles.ProfileYearOfBirth ASC $limit
This will output all Stallions, etc. next to each other.
To also get the breeders contact, you need to join with the contacts table again, using an alias:
LEFT JOIN contacts AS owners
ON profiles.ProfileOwnerID = owners.ContactID
LEFT JOIN contacts AS breeders
ON profiles.ProfileBreederID = breeders.ContactID
To further expand on what #cularis stated, group by is for aggregations down to the lowest level of "grouping" criteria. For example, and I'm not doing per your specific tables, but you'll see the impact. Say you want to show a page grouped by Breed. Then, a user picks a breed and they can see all entries of that breed.
PonyID ProfileGenderID Breeder
1 1 1
2 1 1
3 2 2
4 3 3
5 1 2
6 1 3
7 2 3
Assuming your Gender table is a lookup where ex:
BreedGenderID Description
1 Stallion
2 Mare
3 Geldings
SELECT *
FROM profiles
INNER JOIN prm_breedgender
ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
select
BG.Description,
count(*) as CountPerBreed
from
Profiles P
join prm_BreedGender BG
on p.ProfileGenderID = BG.BreedGenderID
group by
BG.Description
order by
BG.Description
would result in something like (counts are only coincidentally sequential)
Description CountPerBreed
Geldings 1
Mare 2
Stallion 4
change the "order by" clause to "order by CountsPerBreed Desc" (for descending) and you would get
Description CountPerBreed
Stallion 4
Mare 2
Geldings 1
To expand, if you wanted the aggregations to be broken down per breeder... It is a best practice to group by all things that are NOT AGGREGATES (such as MIN(), MAX(), AVG(), COUNT(), SUM(), etc)
select
BG.Description,
BR.BreaderName,
count(*) as CountPerBreed
from
Profiles P
join prm_BreedGender BG
on p.ProfileGenderID = BG.BreedGenderID
join Breeders BR
on p.Breeder = BR.BreaderID
group by
BG.Description,
BR.BreaderName
order by
BG.Description
would result in something like (counts are only coincidentally sequential)
Description BreaderName CountPerBreed
Geldings Bill 1
Mare John 1
Mare Sally 1
Stallion George 2
Stallion Tom 1
Stallion Wayne 1
As you can see, the more granularity you provide to the group by, the aggregation per that level is smaller.
Your join conditions otherwise are obviously understood from what you've provided. Hopefully this sample clearly provides what the querying process will do. Your group by does not have to be the same as the final order... its just common to see so someone looking at the results is not trying to guess how the data was organized.
In your sample, you had an order by the birth year. When doing an aggregation, you will never have the specific birth year of a single pony to so order by... UNLESS.... You included the YEAR( ProfileYearOfBirth ) as BirthYear as a column, and included that WITH your group by... Such as having 100 ponies 1 yr old and 37 at 2 yrs old of a given breed.
It would have been helpful if you'd provided details of the table structure and approximate numbers of rows. Also using '*' for a SELECT is a messy practice - and will cause you problems later (see below).
What version of MySQL is this?
apparantly need to reorganise my query to accomodate GROUP within my primary SELECT clause
Not necessarily since v4 (? IIRC), you could just wrap your query in a consolidating select (but move the limit into the outer select:
SELECT ProfileGenderID, COUNT(*)
FROM (
[your query without the LIMIT]
) ilv
GROUP BY ProfileGenderID
LIMIT $limit;
(note you can't ORDER BY ilv.ProfileYearOfBirth since it is not a selected column / group by expression)
How many records/columns do you have in prm_breedgender? Is it just Stallions, Mares, Geldings...? Do you think this list is likely to change? Do you have ponies with multiple genders? I suspect that this domain would be better represented by an enum in the profiles table.
with the intention of being able to list a pony's Owner and Breeder,
Using the code you suggest, you'll only get returned instances where the owner and breeder are the same! You need to add a second instance of the contacts table with a different alias to get them all, e.g.
SELECT *
FROM (
SELECT *
FROM profiles
INNER JOIN prm_breedgender
ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN contacts ownerContact
ON profiles.ProfileOwnerID = ownerContact.ContactID
INNER JOIN prm_breedcolour
ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
) ilv LEFT JOIN contacts breederContact
ON ilv.ProfileBreederID = breederContact.ContactID
ORDER BY ilv.ProfileYearOfBirth ASC $limit