What is the right way to make a database searchable? - php

I'm trying to make my database searchable. Some of my tables are products, categories, subCategories manufacturedYear, model, brands. I'm using "%LIKE%" on my query and then union on tables. I get a result back, but one of the problems I cannot get the table name. One solution I thought of is creating a searchable table with 4 columns:
id, searchable term (it could be a sku,category ,subcategory etc ) and ID of the searchable term.

You can do this:
...UNION ALL SELECT 'mytable' AS tablename, term FROM mytable ... Basically, you can put a simple string value in with a virtual field in each sub select of the union. In this case I just called it tablename with a value of "mytable" the name of this example table.
So each row in mytable will have a field added named tablename with a string value of mytable. Then as you union the results, each result set (Sub-Select) could have a different string value (in tablename) in the query for their respective tables.
You can also refer to this post which is specifically on "virtual columns"
How to create virtual column using MySQL SELECT?
I don't know if I would say this is the "Right" way, but it will surly work.
Enjoy

Since you are using UNION already, why not just add the name of the table being queried as a column in each UNIONed subquery, like :
SELECT 'table1' AS table_name, id, field1, field2
FROM table1
WHERE value LIKE ?
UNION
SELECT 'table2' AS table_name, id, field11, field12
FROM table2
WHERE value LIKE ?
UNION
...
When you get the data, you can get the name of the table from where each record was returned by checking column table_name.
NB : it would also be a good idea to return the primary key of each row, to make possible further requests easier.

Related

MYSQL/PHP - Select all columns from table records distincting by one column

I'm a bit confused about DISTINCT keyword. Let's guess that this query will get all the records distincting the columns set in the query:
$query = "SELECT DISTINCT name FROM people";
Now, that query is fetching all the records distincting column "name" and at the same time only fetching "name" column. How I'm supposed to ONLY distinct one column and at the same time get all the desired columns?
This would be the scheme:
NEEDED COLUMNS
name
surname
age
DISTINCTING COLUMNS
name
What would be the correct sintaxis for that query? Thanks in advance.
If you want one row per name, then a normal method is an aggregation query:
select name, max(surname) as surname, max(age) as age
from t
group by name;
MySQL supports an extension of the group by, which allows you to write a query such as:
select t.*
from t
group by name;
I strongly recommend that you do not use this. It is non-standard and the values come from indeterminate matching rows. There is not even a guarantee that they come from the same row (although they typically do in practice).
Often, you want something like that biggest age. If so, you handle this differently:
select t.*
from t
where t.age = (select max(t2.age) from t t2 where t2.name = t.name);
Note: This doesn't use group by. And, it will return duplicates if there are multiple rows with the same age.
Another method uses variables -- another MySQL-specific feature. But, if you are still learning about select, you should probably wait to learn about variables.

MySQL Procedure (help)

I have several tables in my database such as
comments
status
events
I’m trying to create an SQL query procedure which counts data from these different tables based on the userID entered and then sum up the counts to create a unique valued. This is what i’ve tried so far but i’m having problems with the syntax. Where am I going wrong??
SELECT COUNT(user_id) AS comments FROM comment
WHERE user_id= userID
UNION ALL
SELECT COUNT(creator_id) AS events FROM event
WHERE creator_id=userID;
In a union, the fields are combined based on order. So giving the count field a different name in each part of the union does not make two fields. It becomes the same field in the end. To differentiate which value came from which table, add a hardcoded string literal like so:
SELECT COUNT(user_id) AS rows, 'comment' as tablename FROM comment
WHERE user_id= userID
UNION ALL
SELECT COUNT(creator_id) AS rows, 'event' as tablename FROM event
WHERE creator_id=userID;

Common Column Names from Multiple Tables MySQL

Hi i have multiple tables of products like product_shirt, product_pants etc and on top of it there is a general product table. Anyways i have came to a situation where i need to make a UNION query by selecting some fields from all the tables that are common like . color field is both in shirts and pants and bags etc ...
Is there any query for getting column names that exist in multiple tables ? I am trying to google it but haven't found any related result
Thanks
Updated:
Table names : prod_rugs, prod_furniture, prod_accessories, prod_generic.
Colum names : color, size and (I want to know all the column names that exisits in all above mentioned table)
I do not need the value of color or size... i need to know which fields are common among these tables
Yes, you may use INFORMATION_SCHEMA for that:
SELECT
column_name,
table_name
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_schema='test'
GROUP BY
column_name
HAVING
count(1)>1
You should replace test to name of your database, or course. Query above will result in column names and table names, which occur more than once - so, more, than in one table (because, obviously, one table can not have column with same name more than once)
If you want some specific table & columns name, then add corresponding condition:
SELECT
column_name,
table_name
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
-- database name:
table_schema='test' &&
-- table names:
table_name IN (
'prod_rugs',
'prod_furniture',
'prod_accessories',
'prod_generic'
) &&
-- column names:
column_name IN (
'color',
'size'
)
GROUP BY
column_name
HAVING
count(1)>1
You need to use sql join to do this.Check this link
http://www.w3schools.com/sql/sql_join.asp

Uniquely identify same column name of two different table after join - mysql

I have 2 tables. suppose a & b
a has id, name, roll. b has id,group,name
This name column data are not same. How can I select and uniquely identify them?
I know about
SELECT a.id,a.name,a.group FROM a,b ............
I know this. But this is an example. I am working with huge amount of data with 20-30 columns in each table. So I don't want to write the column names I need to select rather I want to write the names that I want to exclude.
Like
SELECT * Except b.name............
OR is there any way to uniquely identify after join. Like
.......... a,b WHERE a.name as name1
Please don't ask why those column names are same. I admit it was a mistake. But it's already implemented and heavily used. So finding another way. Is there any simple way to exclude a column while merging them?
Well, you can't write the names you wish to exclude. That is not how SQL works.
However, if writing out 20-30 column names is that much of a burden, you can use information_schema.columns. I write it that way, because 20-30 column names is not particularly large and writing them out is probably less effort than writing the question.
But, back to the solution. It looks something like this:
select concat(c.column_name, ' as ', 'a_', column_name, ', ')
from information_schema.columns c
where table_name = 'a' ;
You might want to include the table schema as well.
As an IDEA, what you can do is, if you want to avoid columns of specific table & your statements have multiple table, you can try following,
Suppose you have 20 columns in table a & 5 columns in table b, you want to avoid col2,col3 & col4 of table b. Standard method is that you should write name of all columns of table a & required columns of table b. But you can avoid to write long list of 20 columns of table by writing a.* & then type required columns of table b. Please see below statement.
Select a.*,b.col1,b.col4,b.col5 from a,b
But if you require to exclude some columns from both table, then I think there is no other way than writing all required column names from both table.
There is no way to exclude a column in SQL SELECT Statement, you can only select a column. You can give alias name to columns while selecting them like below, so that you can identity columns using those alias names.
SELECT a.id as [column1],a.name as [column2],a.group as [column3] FROM a,b ............
There is no way to exclude a specific column but you can avoid to write all columns name and easy your job by below steps-
Step1: Execute below query-
SELECT a.*,b.* FROM a,b ............limit 1;
Step2: Export it into csv format with headings.
Step3: Copyp first (heading) row from csv.
Step4: Delete columns, those are not required and use other columns in your query.
There's only one waY i could see-
first create a temorary table
CREATE TEMPORARY TABLE IF NOT EXISTS mytable
(id int(11) NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM;
then put your column in temporary table-
SELECT * INTO mytable
FROM YourTable
/* Drop the cloumns that are not needed */
ALTER TABLE mytable
DROP COLUMN ColumnToDrop
/* Get results and drop temp table */
SELECT * FROM #TempTable
DROP TABLE #TempTable

How do I find which of two tables has the row with a given value in a known column

If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;

Categories