I know this is a very poor database design,but still i want to know is there any way to search a keyword in all the columns in that table through Mysql query(not Php)?
I've searched in internet but didn't find any help regarding this? if nothing such possibilty is there,then i think dividing the table is only the option left
You cannot use wildcards and anything like that on column names. Either select all columns with *or name the ones you need.
You could use dynamic SQL to patch your query together but this will probably only give you more problems handling it.
Related
i have an SQL table that you can add brand names to, and when a new brand name is added, it will either increase the existing brand active count, or create a new brand name.
The problem is, if someone added a new brand with different spelling (like adding Toyota but spelled toyota) it would make a new brand with a new active count and new brand id. Now that the table has a few instances of this, is there a way i can sort through with SQL and merge the similar brands? I know this would end up deleting a few rows and I'm not sure if SQL has the power to do this all at once.
I'm still kind of new to SQL so any advice on this is appreciated. I heard that using Python Pandas would be easier so I am currently looking into that for a method to do this.
In case of simple case changes, you can use functions like LOWER() to convert all of them to lower case and then group results together based on brand_name,
However, your question says "similar" records where similar is not so well defined. The SQL language expects you to clearly define what you need.
If you are looking to fix one / few characters you can use LIKE operator with percentage (%) and / or underscore (_) sign. You can define all permutations of errors you would like to identify by placing % and _ at various positions. Alternatively, you can also explore SOUNDEX function or sounds like in MySQL and see if you can merge brand names based on SOUNDEX.
If data is not huge, I will suggest you to create another table / temporary table to perform such operation. This way, you can always refer back to original data.
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?"
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 want to query everything, Like:
SELECT * FROM
but I want to exclude two columns because their not necessary, but there's too many columns I need to just type it all one by one. Is there an exclude keyword or except keyword or something in SQL or MYSQL?
No. It's better practice to type out all of the fields as opposed to SELECT * FROM ... anyway.
If you're going to be a programmer, you may as well get used to typing :)
Yes, you must type them all out.
Or, if there really are that many you can do it programatically.
SHOW COLUMNS FROM `tablename`
Will give you the list of columns. Use whatever language you are using to pull them out and build the query with them.
Of course, you have to make sure the column names are escaped (what if you have a column name called select?).
Alright, I'm trying to write a query to display all the tables that contain a certain prefix. Something like what is displayed below (but is obviously incorrect)
SELECT TABLES LIKE chat_
So any table that has the chat prefix, would be displayed. I plan on formatting the output, so it's not going to be a raw output, and I also understand that "what idiot would display table names publicly", and security measures are being taken to prevent that "accidental" table drop (just trying to avoid a flame war). So, how is this accomplished?
You can also use regular expressions, which allows a little more flexibility (though a performance cost):
SHOW TABLES WHERE tables_in_db REGEXP 'chat.*';
In this example, replace db with the database name of concern.
SHOW TABLES LIKE 'chat_%';
You need to add "in some_db first" before where like below
SHOW TABLES in test_server_service where 'table' regexp 't_*';