I have 3 tables UPDATES, MODELS, and UPDATES_MODELS.
The UPDATES table has the following columns:
update_id,
posted
thumbnail
The MODELS table has the following columns:
model_id
first_name
last_name.
The UPDATES_MODELS is a linking table and has the following columns:
update_id
model_id.
Some updates have more than 1 model. For example if an update has 2 models, my query will return 2 duplicate thumbnails, 2 duplicate titles,
2 duplicate dates, and 1 update will have 1 model name and the other update will have the other model name.
This is not what I want.
An update with 2 or more models should return 1 thumbnail, 1 title, 1 date and list both model names with the update.
I tried using DISTINCT in SQL query, that did nothing. I got the same result with or without the DISTINCT keyword.
Here's is my code:
SELECT updates.update_id, title, posted, duration, updates.thumbnail, updates.alt_tag, updates.title_tag, updates.visible, models.model_id, first_name, last_name, updates_models.update_id, updates_models.model_id
FROM updates
INNER JOIN updates_models
ON updates.update_id = updates_models.update_id
INNER JOIN models
ON models.model_id = updates_models.model_id
First of all I doubt you table model. If you have the case 'Update have multiple Models, but not same Model can own to multiple Update', then it's one to many relation ship and you can just keep update_id in Models table.
Your question is not very clear and incomplete. Your query has columns which you have not explained in the question. However you can try below query for your case if I got you properly with limited details.
SELECT updates.update_id, posted, updates.thumbnail,array_agg(first_name), array_agg(last_name), updates_models.update_id, updates_models.model_id FROM "UPDATES" updates INNER JOIN "UPDATES_MODELS" updates_models ON updates.update_id = updates_models.update_id INNER JOIN "MODELS" models ON models.model_id = updates_models.model_id group by updates.update_id, posted, updates.thumbnail, updates_models.update_id, updates_models.model_id
Related
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 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.
I've created some tables in MySQL and I'm trying to select the data that's in those tables so that they get displayed. I have the following tables. Formed, Track, Album, Band, Customers and I have got the primary and foreign keys within the tables. I've also used the Insert into statement to insert data into the tables so there is data within the tables. I'm trying to get all the albums from a certain band to be displayed and I'm following the example below
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Which can be found at W3Schools, to produce the following statement:
SELECT Album.AlbumID, Album.Title, Band.BandName
FROM Album
INNER JOIN Band
ON Album.BandID = Band.BandID
ORDER BY Band.BandName;
And this statement that I've made does create the table headers but doesn't display any data as shown here.
EDIT
In all the tables I've created, I hav got the primary key as an int AUTO_INCREMENT
EDIT #2
I've now changed it so that I have:
SELECT Album.AlbumID, Album.Title, Band.BandName
FROM Band
LEFT JOIN Album
ON Album.BandID = Band.BandID
ORDER BY Band.BandName;
Which displays only the band name and gets a NULL values for the AlbumID and Title
EDIT #3 Completed
I've managed to edit it so now I have the script working. It's as follows:
SELECT Album.AlbumID, Album.Title, Band.BandName
FROM Album
LEFT JOIN Band
ON Album.BandName = Band.BandName
ORDER BY Band.BandName;
I have an SQL table data as follow
I want to display single record for product
example
90792 Amlaan-Hi-Power .............. Show only 1 record when there are 2 record
90793 Amlaan-Neutral .............. show only 1 record when there are 2 record
90794 Amlaan-Phosphate free .........show only 1 record when there are 2 record
90801 Acetone .......................show only 1 record when there are 2 record
90901 Acetanilide ...................show only 1 record when there is 1 record
Can I do this using Inner join
I know
select distinct product from product ORDER BY `product`.`product` DESC
will select distinct (unique) product code and that to only one field i.e. product but confused how to get other information using SQL statement
but results in duplicate records or same table...........................
It looks like your duplicate rows vary by the quantity of product in the package.
You can display just the product and name with
SELECT DISTINCT product, name
FROM product
If you want to deal with the quantity as well, that's a little trickier. This might work: it will put all product codes on one line.
SELECT product,
GROUP_CONCAT(product_code ORDER BY product_code) product_codes,
name
FROM product
GROUP BY product, name
Self join doesn't make a whole lot of sense for this application.
Use group by option for such purposes.
SELECT product,GROUP_CONCAT(product_code SEPERATOR '|') AS product_code,name FROM Table GROUP BY NAME
It will show only one record for duplicate names.
The multiple enteries of product code will seperated by | .
I have a database of articles, which are stored in categories. For my homepage, I want to grab an article from each category (I don't care which). However, some articles are crossposted to multiple categories, so they come up twice.
I have a table called tblReview with the article fields (reviewID, headline, reviewText) and a table called tblWebsiteContent that tells the site which categories the articles are in (id, reviewID, categoryID) and finally, a table called tblCategories (categoryID, categoryName) which stores the categories.
My query basically joins these tables and uses GROUP BY tblCategory.categoryID. If I try adding 'tblReview.reviewID' into the GROUP BY statement, I end up with hundreds of articles, rather than 22 (the number of categories I have).
I have a feeling this needs a subquery but my test efforts haven't worked (not sure which query needs to contain my joins / field list / where clause etc).
Thanks!
Matt
SELECT T.categoryName, tR.headline, tR.reviewText
FROM (
SELECT tC.categoryName, MAX(tR1.reviewID) reviewID
FROM tblReview tR1 join tblWebsiteContent tWC on tR1.reviewID = tWC.reviewID
join tblCategory tC on tC.categoryID = tWC.categoryID
GROUP BY tC.categoryName) T JOIN
tblReview.tR on tR.reviewID = T.reviewID
this query will select for each category an article headline corresponding to the Max reviewId for that category (you said 'I don't care which')
Try using SELECT DISTINCT. (This will only work if your SELECT is only pulling the article ID.)
select DISTINCT reviewID