Show two fields from two tables as one field in mysql LIKE - mysql

I want to implement like search for autocomplete.I have two tables,Location and SubLocation.
I want to return only one field for this like.Here is my query
SELECT l.loc_name,sl.sub_loc FROM Location l,SubLocation sl
where l.loc_name LIKE '$term%' OR sl.sub_loc='$term%'
I want to show matching result from both tables as one return.EG,if i type D so i can view Dubai from first table and DubaiMarina from second table as one coloumn

You can use UNION
SELECT l.loc_name FROM Location l
where l.loc_name LIKE '$term%'
UNION
SELECT sl.sub_loc FROM SubLocation sl
where sl.sub_loc='$term%'
If the tables do not have duplicates, you can replace UNION with UNION ALL as the union all option will not look for duplicates, it might be a little faster depending on the amount of data in returned by the queries.

Related

Filter table records based on others tables

I am having a table "all_data" which consists 2 set of records
a) Independent records which should get fetched all times
b) All records from table "all_product"
Now I am also having few more tables like
'MS_product', 'apple_product' ,'Linux_product' and all these tables are subset of table 'all_product'.
I already have used left or right join but looks like this will not be usefful.
I want to fetch all the Independent records from table 'all_data' and only matched records from other tables i.e. 'ms_product' so final output should have
all independent records + matched record from 'MS_product' or 'apple_product' or 'Linux_product' table.
You can use the MINUS operator for the first query :
SELECT *
FROM all_records
MINUS
(
SELECT *
FROM MS_product
UNION
SELECT *
FROM Apple_product
)
It will gives you all the tuples who are not in the MS_product and Apple_product tables.
However I don't recommend to design your database like you are doing. Prefer a single table with a dedicated column for the brand product.

Unpivot multiple grouped columns in MySQL

I am trying to unpivot multiple groups of columns with the same attribute but multiple value columns.
Say there are 2 products and 3 customers.
I am trying to get a transformed table with 1 attribute(customer: customer1, customer2 and customer3) and 2 values (product1 and product2)
I tried spliting it into 2 tables and then unpivot each table and finally join both the tables. I believe this is an unreasonable approach.
I have also done using multiple selects with union all. The place where I am getting stuck is how will I fill the customer column as all the fields are numerical values and the customer column which is to be formed is categorical.
SELECT ID-1, ID-2, ID-3, product1_customer1 AS customer1, product1_customer2 AS customer2, product1_customer3 AS customer3
FROM table
UNION ALL
SELECT ID-1, ID-2, ID-3, product2_customer1, product2_customer2, product2_customer3
FROM table
How can I get the product column?
Can you please point out in the direction where I'm going wrong?

Multiple tables SQL with 1 same column

I have 2 tables:
acco_info
acco_revenue_2016
Both tables have different columns except for 1, acco_id.
This column is what connects both tables.
I want to write a query that combines important data from both tables and links them to the acco_id.
So from the acco_info table I need the following columns:
acco_id, acco_name, region_name, country_name
From the acco_revenue_2016 table I need:
acco_id, sales, revenue_per_item, revenue
The output should look like this:
acco_id, acco_name, region_name, country_name, sales, revenue_per_item, revenue
What's the best way to write this query?
I am stuck on
SELECT acco_id FROM acco_info UNION SELECT acco_id FROM acco_revenue_2016
This joins the ID's together but I can't find a way to also show the other data.
You'll be looking for something like this;
SELECT
ai.acco_id
,ai.acco_name
,ai.region_name
,ai.country_name
,ar.sales
,ar.revenue_per_item
,ar.revenue
FROM acco_info ai
INNER JOIN acco_revenue_2016 ar
ON ai.acco_id = ar.acco_id
This is assuming that both tables contain the same acco_id. Notice the table aliases to see which field is coming from each table

combine mysql tables with query UNION or JOIN?

brand new to complex mysql queries. I have a database schema that includes a pair of tables that I need to combine:
In the first table avatars, I have columns user_id, url, and name, with a single row for each unique user_id. In the second table interpretations I have user_id and scan_index plus 3 other fields. I'd like to generate a mysql result that matches a specific scan_index from interpretations, includes all columns from this table and additional corresponding fields url, and name from avatars. Should I be using JOIN or UNION to do this query?
JOIN is used to combine both tables on the basis of column values (here in your case, i guess, its user_id), your query can be something like this :
SELECT url, name, scan_index,...
FROM avatars
LEFT JOIN interpretations
ON avatars.user_id = interpretations.user_id
WHERE interpretations.scan_index = *your_specific_value*
You need to use join. To explain it very simply, a join combines 2 tables by placing the data from the 2 tables next to each other, while a union combines 2 queries by placing the data below each other.

Join multiple fields from the same table

I wanted to know if it was possible to make a select on a table that contains multiple field and join them in 1 result :
Example :
Table :
id
dayOne_City
dayTwo_City
dayThree_City
Result : one column that contains the rows of all the cities (Distinct).
2) Am i better to do a view if i have a lot of query to that specific list ?
3) should i do 3 select with union ?
Thank you
You should be fine with:
select dayOne_City from YourTable
UNION
select dayTwo_City from YourTable
UNION
select dayThree_City from YourTable
However, you should review your design to allow multiple cities per whatever-is-that-your-table stores. That is, create an actual many-to-many relationship by creating an intermediate table between YourTable and Cities.
select concat_ws(',', id, dayOne_city, dayTwo_city, dayThree_city, etc...) as allInOne
Details on the function used here. However, I should ask why you're doing this. By joining the fields together, you're destroy any chance of reliably extracting/separating the data again later. Only do this kind of "bulking" if you never plan on using separate portions of the data elsewhere based on the results of this query.