Is it possible to check if any of the field in a mysql row is empty? - mysql

Ran into a brick wall & I came running here for help.
Is it possible to check if a field is empty in a given mysql table row and no, I do not want to check by each column name ( with a single line sql if possible).
I would elaborate some more:
say if I have a table with columns t1_col1,t1_col2 and ...
so I would like to know if any of these columns is empty.
and if I have another table with columns t2_col1, t2_col2 and ..
I would like to use the SAME sql statement to check if any of these columns are empty.
I have not tried anything, because I do not know what to try, I know it is possible to achieve this by checking if column are null ( if column names are known that is and also I know column names of table can be found by 'show column' of mysql). So those are not the way I want to go. I want to know if there is any single command that can do this checking ?
Can any body help me please.

There is no such thing in MySQL. You will have to check each column.
However, it is possible to get the table schema like this:
DESCRIBE table_name;
It will give you a list of all columns in the table, then using your favorite programming language, you can cycle over all fields to create a query to check if they are empty.

Related

MySQL/Doctrine - Search in all columns and joined tables

I have a table in MySQL database with about 30 text fields and about 10 joined N-N tables.
My client wants one form input field to search through all the data.
Is there an easy way to do it?
My assumption is that if I do so many joins, the query is going to take ages.
So an idea I had is to create a column called "ALL". After each edit/add action I would dump all the other columns' date into this ALL column and do a search like this:
Select * From Table WHERE all like "%search"
Is it possible to do it like this? Anyone knows the right way to do it?
Thank you, Mike.
Yes, right
Commonly, there is another (distinct) column 'all' that is a tuple of all values of all columns and then you search through that column.
Another option is to add a different database just for a sake of fulltext
https://www.elastic.co/
https://www.algolia.com/

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?"

How do I create a table name in MySQL using a select?

I'm building a MySQL event to make a copy of a table in the database with a timestamp in the name.
CREATE TABLE `db_name`.`tbl_prefix_(SELECT TO_SECONDS(NOW()))` ( [the rest...]
Obviously this isn't working. What should I do to make it work?
Any suggestions are welcome.
Thanks
This is a bad architecture. Generating tables on the fly is not something you should do.
Instead, create a single table with a timestamp column. For instance, if you would before have 3 tables with three timestamps A, B, and C, you now have one table with a timestamp column containing the values A, B, and C, respectively.
In order to do this, you would need to use "dynamic SQL". That is, make use of the MySQL PREPARE statement.
What you'd need to do is populate a variable with a string that contains the SQL text you want to execute. Doing variable substitution into string is trivial.
The "trick" is to take that dynamic string and execute it like it was a SQL statement.
And that's what the PREPARE statement does for us, takes in a variable, and reads the contents of that variable like it were a SQL statement.
With that said, rather than give an example code that demonstrates this in more detail, I'm going to suggest that you re-think this idea of creating a table with timestamp value as part of the name.
What problem is that designed to solve? And carefully consider whether the proposed design for a solution will introduce a bigger problem than it solves.

Replace row names in an SQL statement with informations from an other table

I have a MySQL table named data where the name of a column is field_id_# (where # is a number from 1 to 129). I also have another table named fields with columns field_id (with only the # of the corresponding field in table data - That means just the number # not field_id_#) field_name and field_label. Now I would like to run a query like this:
SELECT data.field_id_1 AS fields.field_label1,
data.field_id_2 AS fields.field_label2
[...]
I don't know if this is possible or not and if so, how to do it.
Can someone help me with that?
Thanks for your help.
Please use the meaningful names directly for your column definitions! All your selects and program parts that access the database will be readable. They're surely not readable with the naming convention you describe here.
For an immediate solution you can build the SQL Statement dynamically/programmatically, or you generate views where the columnnames are replaced with the meaningful ones.

Create columns that autoincrement with name in a MySQL Database

I know that this might seem like a strange question, but let me try and explain it. I have a database table called 'plan' and in it the first column is called 'username' and the columns after it are called 'question1', 'question2' and so on. I now need to add a hundred or so more columns named like this, but it would be nice to have a sql statement that would automatically do that for me.
I know this wasn't set up in the best way, but if you have a solution, please let me know :)
There isn't any SQL command or feature that would do this automatically; sure you can generate the alter table statements and add the columns programmatically; however, your design would be terribly flawed.
Instead of adding columns, you should create a table containing the question, the user_id (or username, whatever is the PK) to hold the records. If you need to identify a question by number (or ID), simply add another column called question_id.
Write the query in sql to excel. Seperate the incrementing number. Drag down until excel row 100. Hard to explain but i guess you ll figure it out. You'll have 100 incrementing add column sql statements. copy paste run it on a query tool.