Champak Posted August 30, 2023 Share Posted August 30, 2023 I want to group elements from a mysql query, but the way I'm thinking of going about it seems inefficient and I'm checking if there is a better way. I'm querying a table column with skus that have one letter, 1 to 3 digits, a dash (which we'll call the prefix), and then 7 digits. So it can look like A3-0000000, C12-2323323 or J143-7829312. I want to get the count of each of the prefixes (A3, C12, J143, etc.). Let's assume I don't know the prefixes or how many different ones there are. My thinking on how to do this is: 1/ Query the database and sort all the sku. (before someone says I should do this all in the query, I have no idea how to do that if it's possible) 2/ loop through the array with a stringsplit to get just the prefixes. 3/ remove the duplicates (this will now be my prefix array). 4/ loop through the original array with the prefix array with an _arraysearch to get the count of the skus with each prefix. 5/ put that count next to the prefix in the prefix array (multi-d array). Is this the best way to go about this? Link to comment Share on other sites More sharing options...
jchd Posted August 30, 2023 Share Posted August 30, 2023 (edited) A better way? Yes: SQL itself! Given a table like this: CREATE TABLE "SKUs"("SKU" CHAR); loaded with sample data: SKU A3-0000000 C12-2323323 J143-7829312 B54-5487451 A3-4875962 C12-1234567 C12-7654321 F40-7894560 A3-7894562 A3-4561238 just run the following query: select SUBSTR (sku, 1, INSTR (sku, '-') - 1) "Prefix", count(*) "Counter" from skus group by prefix order by prefix; and you get this: Prefix count(*) A3 4 B54 1 C12 3 F40 1 J143 1 I used SQLite string functions, I've f*ckall idea how they translate into MySQL dialect. Edited August 30, 2023 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Danp2 Posted August 30, 2023 Share Posted August 30, 2023 From here -- select substring_index(sku,'-',1) prefix, count(*) from yourtable Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Champak Posted September 1, 2023 Author Share Posted September 1, 2023 Thanks both of you. This worked. "SELECT substring_index(`sku`,'-',1) 'Prefix', count(*) 'Counter' from `roottesCart_product` where quantity = 1 group by `Prefix` order by `Prefix`;" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now