MySQL SELECT table.column AS table - mysql

I would like to know if it is safe to do something like this in MySQL queries:
I have a table level (id,label,etc)
SELECT level.label AS level FROM level
I mean
SELECT table_name.column2_name AS table_name FROM table_name
Actually I'm calling "level" table from another table by a "JOIN", but I made it simpler to post it; and it works!
But I wanted to know if this will not cause some issues later.

You should be perfectly fine doing this. I can make queries difficult to read - more complex ones than this, at any rate - so I wouldn't call it best practice, but if that's an appropriate name then use it. Another name might be LabelOfLevel which would get rid of the problem altogether....
Cheers -

Related

Remove variable part of string in Mysql Workbench

Halo all.
I have a table with two columns. One of them with customer_id and another one with acceptence_reason. I want to delete the customer_id from the acceptance part but have not yet come up with a effective way to do so.
To avoid confusion I have added a picture to clearify my problem
I suppose you already tried substr() but maybe you didn't know instr(). So I would try that:
select Customer_id, substr(acceptance_reason, 0, instr(acceptance_reason,"(") ) as acceptance_reason from ...

SQL: Extract column headings from Dynamically Generated Table

After selecting data from multiple tables, like this:
SELECT games.name, scores.score
FROM games, scores
WHERE players.id = scores.player_id
..can I extract the column headings of this newly generated table?
The statement I'd normally use would be as follows:
SELECT column_name
FROM information_schema.columns
WHERE table_name=table
But naturally this would not work for a dynamically generated table with no name
Help much appreciated!
Edit: I'm using the MariaDB client
..can I extract the column headings of this newly generated table?
No. Mostly because you've not created a table. Just a result set. I think you already know this, because you've already looked at the information schema :)
Unfortunately it seems even creating a temporary table won't help - because those aren't stored in the information schema either. I don't think you can declare cursors for SHOW COLUMN... statements either.
I don't think you've got a way to do it I'm afraid.
If it's a prepared statement (with the select statement held in a variable) you could probably chop it up using some ugly string manipulation...?
It might at this point be worth asking "more abstractly, what problem are you trying to solve?"

PHP Mysql Query HELP PLEASE

I have something like this in a mysql row value:
{15;16;}
And I want a mysql_query to update it. It must be something like it "{15;16;17}" So I need to delete last "}" add my text and close with "}"
to delete last "}" code:
SUBSTRING(server_players, 1, LENGTH(server_players)-1)
to add text this :
CONCAT(server_players, '17;}')
but I don't know how to build a one query :(
It's the query :
UPDATE members SET "Here must be built code" WHERE server_id=1
Generally this sort of manipulation is best done in the application, but additionally, it's not a great idea to store data in that format. Relational databases prefer things to be broken out as individual records.
That said, your solution is to nest the two things:
UPDATE members
SET server_players=CONCAT(SUBSTRING(server_players, 1, LENGTH(server_players)-1), '17;}')
WHERE server_id=1
The query code for removing an arbitrary player from your list will be even more complicated. I strongly suggest not using this schema.
As you can see this is a very, very messy way to do what should be done as:
INSERT INTO server_players (server_id, player_id) VALUES (1, 17)
Where you have a table specifically for the players on a server.
Remember proper relational tables have the advantage of being quick to query because they're indexed, and you can do useful things with that data like apply a JOIN to pre-load other information.

MySql - Select * from 2 tables, but Prefix Table Names in the Resultset?

I'd like to select * from 2 tables, but have each table's column name be prefixed with a string, to avoid duplicate column name collissions.
For example, I'd like to have a view like so:
CREATE VIEW view_user_info as (
SELECT
u.*,
ux.*
FROM
user u,
user_ex ux
);
where the results all had each column prefixed with the name of the table:
e.g.
user_ID
user_EMAIL
user_ex_ID
user_ex_TITLE
user_ex_SIN
etc.
I've put a sql fiddle here that has the concept, but not the correct syntax of course (if it's even possible).
I'm using MySql, but would welcome generic solutions if they exist!
EDIT: I am aware that I could alias each of the fields, as mentioned in one of the comments. That's what I'm currently doing, but I find at the start of a project I keep having to sync up my tables and views as they change. I like the views to have everything in them from each table, and then I manually select out what I need. Kind of a lazy approach, but this would allow me to iterate quicker, and only optimize when it's needed.
I find at the start of a project I keep having to sync up my tables and views as they change.
Since the thing you're trying to do is not really supported by standard SQL, and you keep modifying database structures in development, I wonder if your best approach would be to write a little script that recreates that SELECT statement for you. Maybe wrap it in a method call in the development language of your choice?
Essentially you'd need to query INFORMATION_SCHEMA for the tables and columns of interest, probably via a join, and write the results out in SQL style.
Then just run the script every time you make database structural changes that are important to you, and watch your code magically keep up.

Mysql "magic" catch all column for select statement

Is there a way that I can do a select as such
select * from attributes where product_id = 500
would return
id name description
1 wheel round and black
2 horn makes loud noise
3 window solid object you can see through
and the query
select * from attributes where product_id = 234
would return the same results as would any query to this table.
Now obviously I could just remove the where clause and go about my day. But this involves editing code that I don't really want to modify so i'm trying to fix this at the database level.
So is there a "magical" way to ignore what is in the where clause and return whatever I want using a view or something ?
Even if it was possible, I doubt it would work. Both of those WHERE clauses expect one thing to be returned, therefore the code would probably just use the first row returned, not all of them.
It would also give the database a behaviour that would make future developers pull their hair out trying to understand.
Do it properly and fix the code.
or you could pass "product_id" instead of an integer, if there's no code checking for that...so the query would become:
select * from attributes where product_id = product_id;
this would give you every row in the table.
If you can't edit the query, maybe you can append to it? You could stick
OR 1=1
on the end.
You may be able to use result set metadata to get what you want, but a result set won't have descriptions of fields. The specific API to get result set metadata from a prepared query varies by programming language, and you haven't said what language you're using.
You can query the INFORMATION_SCHEMA for the products table.
SELECT ordinal_position, column_name, column_comment
FROM INFORMATION_SCHEMA.columns
WHERE table_name = 'products' AND schema_name = 'mydatabase';
You can restructure the database into an Entity-Attribute-Value design, but that's a much more ambitious change than fixing your code.
Or you can abandon SQL databases altogether, and use a semantic data store like RDF, which allows you to query metadata of an entity in the same way you query data.
As far out as this idea seems I'm always interested in crazy ways to do things.
I think the best solution I could come up with is to use a view that uses the products table to get all the products then the attributes table to get the attributes, so every possible product is accounted for and all will get the same result