Is it possible to search without first letter? I have some query that using soundex I want to search only ammer without H but seems it is not working to my current query
╔════╦══════════════╗
║ id ║ item_name ║
╠════╬══════════════╣
║ 1 ║ Hammer ║
║ 2 ║ Nails ║
║ 3 ║ Woods ║
╚════╩══════════════╝
SELECT * FROM product WHERE SOUNDEX(item_name) LIKE CONCAT(TRIM(TRAILING '0' FROM SOUNDEX('ammer')), '%')
Expected Result:
╔════╦══════════════╗
║ id ║ item_name ║
╠════╬══════════════╣
║ 1 ║ Hammer ║
╚════╩══════════════╝
Please help me what query is missing?
You can search based on a regular expression
SELECT *
FROM product
WHERE SOUNDEX(item_name) REGEXP '^.ammer'
Select * from table where item_name like '_ammer%'
Related
The title may be confusing, but so is the solution I am trying to SELECT from a table of 173k words a distinct word list by letter.
I already tried selecting distinct letters, running on a mariadb 10.1.37
SELECT DISTINCT LEFT(word, 1)
For example a SELECT shouldnt contain more than 1 word beginning with an "A".
Example Table (Because it's hard to understand)
╔═══════════╦═════════╗
║ Word ║ Result ║
╠═══════════╣ ---- ║
║ Charlie ║ Ava ║
╠═══════════╣ Bianca ║
║ Caddie ║ Charlie ║
╠═══════════╣ ║
║ Brooklynn ║ ║
╠═══════════╣ ║
║ Ava ║ ║
╠═══════════╣ ║
║ Alexander ║ ║
╠═══════════╣ ║
║ Bianca ║ ║
╚═══════════╩═════════╝
You can use the following solution using a GROUP BY on the first character:
SELECT MIN(word) FROM table_name GROUP BY LEFT(word, 1)
You can use MIN or MAX to get the first or last word on every group.
demo un dbfiddle.uk
Okay so basically I am trying to run a simple query with a subquery on phpmyadmin using mysql and it won't stop loading after I run it. The query is:
SELECT t.tagValue FROM tags t WHERE t.tagID IN (SELECT ua.tagID FROM user_taggedArtists ua WHERE ua.userID = 2);
I have ran the individual queries on their own without combining them together and they seem to do what I want, but when I mix them into a subquery form phpmyadmin just loads forever, like I am getting an infinite loop or something.
tags table looks like that:
╔═══════╦═════════════╗
║ tagID ║ tagValue ║
╠═══════╬═════════════╣
║ 1 ║ metal ║
║ 2 ║ alternative ║
║ 3 ║ pop ║
╚═══════╩═════════════╝
etc.
user_taggedArtists table looks like this:
╔════════╦══════════╦═══════╦═════╦═══════╦═══════╗
║ userID ║ artistID ║ tagID ║ day ║ month ║ year ║
╠════════╬══════════╬═══════╬═════╬═══════╬═══════╣
║ 2 ║ 52 ║ 1 ║ 1 ║ 4 ║ 2009 ║
║ 2 ║ 52 ║ 1 ║ 1 ║ 4 ║ 2009 ║
║ 2 ║ 52 ║ 1 ║ 1 ║ 4 ║ 2009 ║
╚════════╩══════════╩═══════╩═════╩═══════╩═══════╝
ect.
Not sure what I am doing wrong here and any help would be greatly appreciated.
Thank you!
Hard to say 'zatly, but "IN", generally, is slow. Try a JOIN and WHERE. I'm going to pretend we can join on tagID. You should have indexes on the join column(s). If not anything you do will perform poorly.
SELECT t.tagValue
FROM tags t
INNER JOIN user_taggedArtists ua ON
t.tagID = ua.tagID
WHERE ua.userID = 2
My table has a list of users with Items associated to them. I hope to be able to consolidate the items field into a list or create another column if the row has a matching User/email.
I have the following structure with in the MySQL table:
╔═══════════╦═════════════╦═════════════╦═════════════╦═════════════╦════════════╗
║ ID ║ User_ID ║ Item ║ Date ║ User ║ Email ║
╠═══════════╬═════════════╬═════════════╬═════════════╬═════════════╬════════════╣
║ 1 ║ 1 ║ Laptop 1 ║ 30th Nov ║ John ║ J#test.com ║
║ 2 ║ 2 ║ Laptop 3 ║ 12th Nov ║ Emma ║ e#test.com ║
║ 3 ║ 2 ║ Camera 3 ║ 12th Nov ║ Emma ║ e#test.com ║
╚═══════════╩═════════════╩═════════════╩═════════════╩═════════════╩════════════╝
I am very new to SQL but i think i would need to use some type of transpose field to a column function?
Any help will be greatly appreciated
Thanks
I think you want group_concat(). Personally, I would use user_id, rather than user/email. or, perhaps all three together. Something like this:
select user_id, user, email, group_concat(item) as items
from table t
group by user_id, user, email;
I have two tables:
╔════════════════╗ ╔════════════════╗
║ ITEM ║ ║ ITEM_TRACK ║
╠════════════════╣ ╠════════════════╣
║ ID ║ ║ ID ║
║ GUID ║ ║ ITEM_GUID ║
║ COUNT1 ║ ║ CONTEXT ║
║ ENDDATE ║ ║ ║
╚════════════════╝ ╚════════════════╝
╔═════╦══════╦════════╗ ╔═════╦═══════════╦══════════╗
║ ID ║ GUID ║ COUNT1 ║ ║ ID ║ ITEM_GUID ║ CONTEXT ║
╠═════╬══════╬════════╣ ╠═════╬═══════════╬══════════╣
║ 1 ║ aaa ║ ║ ║ 1 ║ abc ║ ITEM ║
║ 2 ║ bbb ║ ║ ║ 2 ║ aaa ║ PAGE ║
║ 3 ║ ccc ║ ║ ║ 3 ║ bbb ║ ITEM ║
║ 4 ║ abc ║ ║ ║ 4 ║ ccc ║ ITEM ║
╚═════╩══════╩════════╝ ║ 5 ║ abc ║ ITEM ║
║ 6 ║ aaa ║ ITEM ║
║ 7 ║ abc ║ ITEM ║
║ 8 ║ ccc ║ PAGE ║
╚═════╩═══════════╩══════════╝
What I'm trying to do is fill in the COUNT1 column in ITEM with the count of the number of times ITEM_GUID appears in ITEM_TRACK for all ITEM.GUIDs where ENDDATE is still in the future. I need to do this once an hour for all GUIDS in ITEM.
I can get the counts I need easily
SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;
What I don't know how to do is, how do I merge this with an INSERT INTO statement to automatically update all the items in the items table with the count based on their ENDDATE?
UPDATE:
I have a working solution based on Aquillo's answer:
UPDATE ITEM a
SET COUNT1 = (SELECT COUNT(*) AS total FROM ITEM_TRACK b WHERE b.item_guid=a.guid);
Is there any other way to do this without a subquery?
You can insert from a select like this:
INSERT INTO myTable (foreignKey, countColumn) VALUES
SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;
In case you want to update, try something like this:
UPDATE from SELECT using SQL Server
If you use INSERT INTO you'll put additional rows in your ITEM table, not update the existing ones. If this is what you meant then that's great, but if you want to update the existing ones, you'll need to use update. You do this by joining the table you want to update with the table you want to update from. However, in your case you want to update from an aggregation and so you need to create a table with the aggregated values. Try this:
UPDATE ITEM SET Count1 = temp.total
FROM Item
INNER JOIN (
SELECT ITEM_GUID, COUNT(*) AS total
FROM ITEM_TRACK
GROUP BY ID) AS temp
ON Item.GUID = temp.ITEM_GUID
WHERE ENDDATE > NOW()
I've tried this on SQL Server (using GETDATE() instead of NOW()) to double check and it worked, I think it should work on MYSQL.
My English isn't so good, so I'll give an example.
I have an SQL table that looks like this:
╔═══╦═════════════╗
║ ║ myColumn ║
╠═══╬═════════════╣
║ 1 ║ repeat ║
║ 2 ║ Foo ║
║ 2 ║ Foo ║
║ 3 ║ repeat ║
║ 3 ║ repeat ║
╚═══╩═════════════╝
I want to get the value that repeat most.
foo repeats twice, and repeat repeats 3 times, so I want to get the repeat.
I use mysql engine
This is an aggregation query:
select MyColumn, count(*) as NumRepeats
from t
group by MyColumn
order by count(*) desc;
Add limit 1 before the semicolon, if you only want the value that repeats the most.