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.
Related
I have two tables
table A contain many ids, which one is related to the table B.
I have to make a query on the table A to select only some ids and then take the data from the table B using the selected ids from the table A
How can i do that? (MYSQL)
Based on your description, here is a simple structure of how the query might go, although I recommend you share some sample results & the desired output.
SELECT * FROM tableA
JOIN tableB
ON tableA.id_song == tableB.id_song
WHERE "your condition HERE"
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
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.
I have two tables in which the same field is referenced differently (city_id and geo_name_id) I can't really change this, as they play different roles in both tables. I want to execute the following two queries on both tables and then combine the results into one where the returned field is called city_id :
SELECT city_id FROM user_city WHERE user_id = '$user_id' UNION SELECT geo_name_id as 'city_id' FROM cities WHERE featured = 'yes'
This seems to work fine, however I'm not sure if I have taken the right approach here.
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