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.
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 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.
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
I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.
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.