mysql group_concat in where - mysql

I am having a problem with the following query(if this is a duplicate question then i'm terribly sorry, but i can't seem to find anything yet that can help me):
SELECT d.*, GROUP_CONCAT(g.name ORDER BY g.name SEPARATOR ", ") AS members
FROM table_d AS d LEFT OUTER JOIN table_g AS g ON (d.eventid = g.id)
WHERE members LIKE '%p%';
MySQL apparently can't handle a comparison of GROUP_CONCAT columns in a WHERE clause.
So my question is very simple. Is there a workaround for this, like using sub-query's or something similar? I really need this piece of code to work and there is not really any alternative to use other than handling this in the query itself.
EDIT 1:
I won't show the actual code as this might be confidential, I'll have to check with my peers. Anyway, I just wrote this code to give you an impression of how the statement looks like although I agree with you that it doesn't make a lot of sense. I'm going to check the answers below in a minute, i'll get back to you then. Again thnx for all the help already!
EDIT 2:
Tried using HAVING, but that only works when i'm not using GROUP BY. When I try it, it gives me a syntax error, but when I remove the GROUP BY the query works perfectly. The thing is, i need the GROUP BY otherwise the query would be meaningless to me.
EDIT 3:
Ok, so I made a stupid mistake and put HAVING before GROUP BY, which obviously doesn't work. Thanks for all the help, it works now!

Use HAVING instead of WHERE.
... HAVING members LIKE '%peter%'
WHERE applies the filter before the GROUP_CONCAT is evaluated; HAVING applies it later.
Edit: I find your query a bit confusing. It looks like it's going to get only one row with all of your names in a single string -- unless there's nobody in your database named Peter, it which case the query will return nothing.
Perhaps HAVING isn't really what you need here...

Try
SELECT ...
...
WHERE g.name = 'peter'
instead. Since you're just doing a simple name lookup, there's no need to search the derived field - just match on the underlying original field.

GROUP_CONCAT is an aggregate function. You have to GROUP BY something. If you just want all the rows that have %peter% in them try
SELECT d.*, g.name
FROM table_d AS d
LEFT OUTER JOIN table_g AS g
ON (d.eventid = g.id)
WHERE g.name LIKE '%peter%';

Related

SELECT * FROM games WHERE

I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')

No tables used?

I've been working with this bit of code for some time now, and now, after some tinkering, I thought I finally got it to work, then it gives me this error:
Error Number: 1096
No tables used
SELECT *
and this is the code
SELECT start.*, posts.did, COUNT(posts.pid)
FROM akia_starting_posts AS start
JOIN posts
ON posts.did = start.did
JOIN akia_users AS users
ON users.username = start.username
I'm pretty sure at the * my start is being used, so what is it talking about? It couldn't be any other code in the file, since when I take out this bit of code, it starts working.
Try to add backticks on sql string that has the word start on it. To begin try to use the one table in question first like.
SELECT 'start.*' FROM akia_starting_posts as 'start'
and if it works then you may incorporate it in fullscale join like the one you posted
with bacticks used.

SELECT value AS (desc) not working in subquery

As(no pun intended) you can see in this screenshot the AS statement is not changing the title of the returned query when the AS is located in the subquery.
The right side of the picture is my exp_tables table. The goal here is to figure out what level the player is in each specific skill(so in my Java application I can put it in a nice progressbar). Please let me know if there is something wrong with my SQL for the AS, or if you want to be really helpful, let me know if there is a better way I should be doing this. Thanks so much for your help. Love this site; hoping I can be smart enough to help others soon.
EDIT
Yes sorry for forgetting to upload the code >.<
SELECT
skill_alchemy_exp, ((SELECT exp_tables.id AS alchemy_lvl FROM exp_tables WHERE skill_alchemy_exp < tradeskills LIMIT 1)-1),
skill_axes_exp, ((SELECT exp_tables.id AS axes_lvl FROM exp_tables WHERE skill_axes_exp < weapons LIMIT 1)-1),
skill_baking_exp,((SELECT exp_tables.id AS baking_lvl FROM exp_tables WHERE skill_baking_exp < tradeskills LIMIT 1)-1),
skill_blacksmithing_exp,((SELECT exp_tables.id AS blacksmithing_lvl FROM exp_tables WHERE skill_blacksmithing_exp < tradeskills LIMIT 1)-1),
skill_blocking_exp, skill_blunts_exp, skill_bows_exp, skill_carpentry_exp, skill_cooking_exp,
skill_crossbows_exp, skill_daggers_exp, skill_dark_exp, skill_earth_exp, skill_fire_exp,
skill_foraging_exp, skill_harvesting_exp, skill_healing_exp, skill_hiding_exp, skill_holy_exp,
skill_looting_exp, skill_luck_exp, skill_lumberjacking_exp, skill_milling_exp, skill_mining_exp,
skill_planting_exp, skill_polearms_exp, skill_smelting_exp, skill_swords_exp, skill_wands_exp,
skill_wind_exp
FROM kisnard.characters
WHERE name='Proskier'
Using the "AS" operator on a column inside a sub-query doesn't give an entire sub-query a name. If you look at the left hand part of the picture, you'll notice the column names of the sub-selects are the selects themselves because you didn't give those "columns" a name.
It's hard to tell exactly what it is you're trying to achieve, but you can do something like this... which might be what you want:
SELECT a, b, (SELECT xyz FROM ...) AS c, d, e, ...
That lets you give an alias to the sub-query.

SQL unknown column when doing join

I have been getting an error "Unknown column 'guests_guest.id' in 'field list'" when i try to run the following:
SELECT guests_guest.id
FROM `guests_guest` full join
guests_guest_group
on guests_guest.id=guests_guest_group.guest_id
All the column& table names are correct. in fact, running just
SELECT guests_guest.id
FROM `guests_guest`
works just fine. I suspect there is a syntax issue I am missing. what am I doing wrong?
try:
SELECT gg.id
FROM `guests_guest` as gg
join guests_guest_group as ggg
on ggg.guest_id=gg.id
assuming guests_guest_group does not have an id column.
full join ?
Have you tried simply removing the full?
This not Oracle, it's MySQL, right? AFAIK, FULL JOIN is not implemented yet in MySQL.
The parser (because "full' is not a keyword it knows), evaluates your query as:
SELECT guests_guest.id
FROM guests_guest AS full <--- crucial note
JOIN guests_guest_group
ON guests_guest.id = guests_guest_group.guest_id
After that, guests_guest is not a name it knows, but it uses full as an alias for table guests_guest. That's why this error is produced.
If you really need FULL JOIN and not (INNER) JOIN, then search SO for how to implement FULL JOIN in MYSQL.
#rockerest: I guest so.
Two things I'd look at:
Spelling... I am a fast typer, but sometimes my fingers are dyslexic. Worst case, do a describe on each table and check column names against each other. Or, use the system-confessed column names and copy/paste.
Aliases... but, someone else has mentioned that.
So I guessed just one thing.

Ambiguous left join?

so I'm struggling with what I'm guessing is a very simple problem. I've searched around a bit, but none of the solutions I've found so far have worked on my problem.
SELECT arrangement_ID, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
This naturally produces the "ambiguous error", since the column 'arrangement_ID' is present in both tt_arrangement and tt_vaktliste_vakt. Thinking this was easy to fix, I made the following changes:
SELECT **arrangement_ID.tt_arrangement**, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
However, this produced the error "column doesn't exist". And that's where I'm stuck.
Not sure if it matters, but when using SELECT * the query works as intended. Though that is not really an option for what I'm going to use the query for.
In advance, thanks for any replies.
Prefix the ambiguous column name with the tablename:
SELECT t_arrangement.arrangement_ID, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
(assumes hva, dato are unique column names)
(You can also use aliases, but will still need to prefix ambiguous column names with alias)
You need to give your table names an alias, like below.
SELECT a.arrangement_ID, a.hva, a.dato
FROM tt_arrangement AS a
LEFT JOIN tt_vaktliste_vakt AS v
ON (a.arrangement_ID = v.arrangement_ID)
Not sure is the top lines right as i don't know you table structure so can't know which column comes from where.
Hopes this helps.