Multiple tables SQL with 1 same column - mysql

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

Related

Stacking up rows from tow tables using mysql select statement

I have two tables and wish to join them together so I can query them together. Is there anyway that I can achieve this?
For illustration purpose, see the figures below.
Table Structure
PLEASE NOTE THAT time_enrolled FOLLOWS THE PATTERN ON BOTH TABLES
try this :
Select
id,name,age,time_enrolled,date_joined, null as occupation from students
union
select
id,name,null as age,time_enrolled,null as date_joined, occupation
from parents

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.

Get results of three queries into one table

Here is the situation,
I have an interface with a dropdown(All, Category, Category type, Product) and search button. There are three database tables for each section as CATEGORY, CATEGORYTYPE and PRODUCT
When user select the All option search, I am getting results from all three tables and display on a HTML table.
TO do pagination, I want to fetch results set by set from all the tables.
Here are the three queries I'm using:
SELECT CATNAME,CATDESC,CATSTATUS FROM CATEGORY WHERE CATNAME like %a%
SELECT CATTYPENAME,CATTYPEDESC,CATTYPESTATUS FROM CATEGORYTYPE WHERE CATTYPENAME like %a%
SELECT PRODUCTNAME,PRODUCTDESC,PRODUCTSTATUS FROM PRODUCT WHERE PRODUCTNAME like %a%
From all the tables I'm only fetching the same set of column I want to combine the three queries. I hope my question is understandable. This is not about joining the three tables.. I want the results from all three queries to act as a one set or records and paginate them.
Thanks in advance for any suggestions and please ask if the question is not clear.
The application is JAVA EE based. Struts used and DB is MySQL.
Using view u can do what you want.
in very first u have to change columns heading as same. like
SELECT NAME,DESCRIPTION,STATUS FROM CATEGORY WHERE NAME like %a%
SELECT NAME,DESCRIPTION,STATUS FROM CATEGORYTYPE WHERE NAME like %a%
SELECT NAME,DESCRIPTION,STATUS FROM PRODUCT WHERE NAME like %a%
then you can create view like this
CREATE VIEW V AS
SELECT *
FROM ((CATEGORY NATURAL FULL OUTER JOIN CATEGORYTYPE)
NATURAL FULL OUTER JOIN PRODUCT);
P.S. if you can not change columns heading,you can have to change view according to that tables' columns

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

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.

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.