I have this output for this search, but now i need to have another output that selecting according to all the best provider(NOME_PRESTADOR) for each service area(AREA_TRAB)
So, for example, to canalizador, the best provider is Arlindo Rui so i want to show just Arlindo Rui not Arlindo Rui and Bárbara Raquel, the some to the other areas.
Translating your table names would be helpful
Anyway, you're trying to select the best worker of each category from what I guess
Create a view using the query you posted, and then try something like this:
mysql SELECT best of each category in a single table
It's essentially the same problem
Related
Here is what I would like to do: I have an old vBulletin forum with a couple thousands of threads and replies. I developed a Laravel forum application myself and would like to migrate data from the old forum into the new one, even though the structure is completely different. What is more, in my new MySQL table I use base64 id's that I manually have to program. So I cannot use pure MySQL to fill this in I guess.
Essentially I need to do something like this:
Old table ---- new table
'thread' -> 'title'
'text' -> 'body'
and so on... plus the thing with the base64 id's.
Any idea how to approach this? I didn't quite find anything useful using search, probably because I wasn't looking for the right keywords. Thanks a lot!
Here's how to approach this kind of problem.
Start by doing a SELECT operation to generate a result set looking like your new table's columns. Maybe something like this will work for you.
SELECT thread AS title,
text AS body,
TO_BASE64(SHA2(UUID(), 224)) AS base64id
FROM old_table
(I guessed about what belongs in your base64id column. TO_BASE64(SHA2(UUID(), 224)) generates hard-to-guess pseudorandom values. You can use anything here.)
Once you're satisfied with your resultset, you can do
INSERT INTO new_table (title, body, base64id)
SELECT .... your select query
to put rows into the new table.
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 -
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.
I think my question is best explained via an example: Suppose I want to write a webserver for streaming music. I have the following columns in the songs table:
|song_id|song_name|song_genre|song_length|artist
I would like to enable my users to save playlists, but I don't want my playlists to be defined by explicitly specifying the songs that are in the playlist, rather by something like "all songs by ringo starr", which would mean that when new songs by ringo starr are added to the table, the playlist will automatically have them. Actually what I want is a table called playlists that contains a list of mysql views.
The most naive approach would be to simply have a table called playlists, and one of it's columns would be called playlist_query which would store for the above example something like the string "select song_id from songs where artist='ringo starr'.
This is of course incredibly insecure, but it would suffice in case there is no better solution, since this application is used only internaly inside our company by just a few employees who are all good programmers that know their way around mysql.
So what do you suggest? go with this ugly solution, or is there some easier way to do this?
You could store the name of the view in the playlists table instead of the query itself.
You'd still have to create the required views though and I don't know how that helps your "security problem".
Could you elaborate on what kind of security is required?
I'd define a Playlist as a list of Filters where a filter might be a song ID, an artist ID, a song name, a genre ... I'd then fetch all songs for each filter, either in a single query combining filers with simple OR or using UNION if you want the playlist to be in the same order as the filters. Note though that it needs some additional effort to get results from UNION in the order of the queries, see this answer.
The advantages of this approach:
no millions of views to manage
no SQL queries stored in DB
pretty flexible filtering
Admittedly, I can't say much about the performance of this solution. Shouldn't be too bad though.
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