MySQL/Doctrine - Search in all columns and joined tables - mysql

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/

Related

Access query across multiple tables with potentially non-existent related records

I'm working on an MS Access 2010 database, and I have 5 tables I need to query.
Table 1 has an ID, name, a several other unrelated columns.
Tables 2 through 5 have their own IDs, the ID of the related record from Table 1, a date field, and then other miscellaneous fields.
I am trying to build a query to return the ID and name from table one, and the dates from tables 2-5.
I didn't really think that there would be any issues, but here I am. I have not done anything special with the query, just added the tables/columns to the Query Design view.
The query is working, sort of, however if the user has not created a related record with a matching ID in any of the related tables that record in table a is ignored.
I would like the query to return all records from table 1, and show me the dates from tables 2-5, even if one or all of them do not have a related entry.
I hope this makes sense... Any assistance is greatly appreciated! Thanks! :)
If I correctly understand what you are asking for. Open the query in design view, double click on the line connecting table 1 to another table and select. All from table 1 and only those records...
I think that should give you what you are looking for. Otherwise you will probably need to post some more data about your other tables.

One column or separate columns for extra data - mysql

I was thinking what if I have a table with columns for meta_description (varchar 300), meta_tags (varchar 300), and meta_title (varchar 200)... can I "join" all this columns just into one column "extra_information" (longtext) and save here the same information but maybe in JSON format?
Is this convenient or not and why :)?
This fields are not very important for me, I will never make any query to search or sort the results trough this information. The metatags for example are only a comma separated text I don't need to do some kind of relation table on this.
What I want to know is this will save space on my database or will be working a little bit faster, or things like that... But if you tell me that have 5 columns instead of just one is the same for MySQL of course I will have the 5 columns...
Thanks a lot!
The answer boils down on: Does MySQL have to work with your data?
If all date is concatenated in one column, be it as JSON or comma-seperated or what not, it is nearly off limits for any MySQL operation. You can surely SELECT it, but it is very hard to search, group or sort by anything inside that column. So, it you are absolutly sure MySQL soes never have to see the data itself and will only return some column with data in it, go for it.
Benefits are that the table structure does not have to be changed because your data changes. and column structure is very clean
if you need to filter, sort, group or do whatever operation on it within a SQL query, leave it in seperate columns.

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

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.

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.

Is multiple field index in MySQL a good choice?

I have a huge data set. The structure looks something like this:
K_Field1, K_Field2, K_Field3, K_Field4, D_Field5, D_Field6, D_Field7, D_field8
The problem is that only the first 4 field (K_Field1,K_Field2,K_Field3,K_Field4) together identify a row uniquely. I created one table, using these fields as its fields.
Let's say I have 1 million rows in the table using that structure. If I import a new record, I have to decide if it's already in the database. If it is, then I have to update it, if not, then I need to insert a new row.
To do that, I need to put a multiple field index on the first 4 columns, which is - I'm afraid - not the best solution. Is there a better database structure to store and search in that data, or I have to live with the four-fielded index?
I'm using MySQL
There's nothing wrong with creating an index on the first 4 columns, in fact you should:
create unique index mytable_key on mytable(K_Field1,K_Field2,K_Field3,K_Field4);
because that is the reality of your situation.
It is also the "correct" solution.