SELECT value AS (desc) not working in subquery - mysql

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.

Related

Error: MySQL client ran out of memory

Can anyone please advise me on this error...
The database has 40,000 news stories but only the fields 'story' is large,
'old' is a numeric value 0 or 1,
'title' and 'shortstory' are very short or NULL.
any advice appreciated. This is the result of running a search database query.
Error: MySQL client ran out of memory
Statement: SELECT news30_access.usehtml, old, title, story, shortstory, news30_access.name AS accessname, news30_users.user AS authorname, timestamp, news30_story.id AS newsid FROM news30_story LEFT JOIN news30_users ON news30_story.author = news30_users.uid LEFT JOIN news30_access ON news30_users.uid = news30_access.uid WHERE title LIKE ? OR story LIKE ? OR shortstory LIKE ? OR news30_users.user LIKE ? ORDER BY timestamp DESC
The simple answer is: don't use story in the SELECT clause.
If you want the story, then limit the number of results being returned. Start with, say, 100 results by adding:
limit 100
to the end of the query. This will get the 100 most recent stories.
I also note that you are using like with story as well as other string columns. You probably want to be using match with a full text index. This doesn't solve your immediate problem (which is returning too much data to the client). But, it will make your queries run faster.
To learn about full text search, start with the documentation.

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')

Ignore case in ORDER BY in mysql

I came across a situation where i want to sort the mysql query result in alphabetical order ignoring the case... I mean
Al
aasd
Cfds
ddafsf
...
I found a solution where convert every element of the result into either lower or upper case and use Order By ..I didnt like this approach... http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html .. Read this article..... But i grasped nothing...Just wanna know how can this be done... cheers...
why not use ASC directly,
SELECT *
FROM tableName
ORDER BY columnName ASC

mysql group_concat in where

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%';

Is possible to do this with a single query: get a fixed number of items of different types, ordered

Let's say i have a table with a "type" and a "date" column, and i want to fetch the latest 3 items of each type, ordered by date.(Can't trust the natural table order, or insertion order).
The query doesnt need to calculate all the different values for the "type" column, that can be specified in the query.
I'm trying with variables, like this:
set #c=0;
set #d=0;
select *, #c:=IF(type = 1, #c+1,#c), #d:=IF(type = 2, #d+1,#d) from testtable HAVING((type=1 AND #c < 3) OR (type=2 AND #d<3)) order by testdate;
This is "almost" working, (it's returning one more entry for each type,which is fine), and i guess it's related to way mysql is resolving the HAVING clause (in fact, in some scenarios i'm finding i need to use WHERE instead of HAVING).Can someone shed some light in this? Am i safe using it like this?
Ok, looks that using HAVING is fine...I was having problems because, in my real code, i need to use a few order-by, and the last of those is RAND() (to resolve ties), and that RAND() ordering seems to mess up with the mysql variable assignment.