I'm trying to join 2 tables with the following query:
SELECT board.boardID, board.userID, board.postID, items.Url,
items.Image
FROM board
JOIN items on userID = items.user_id
Which gives me the following result:
However, as you can see, every result is printed double. I tried using SELECT DISTINCT but that is giving the same result.
My mistake,
The query I was looking for is:
SELECT DISTINCT board.boardID, board.userID, board.postID, items.Url,
items.Image
FROM board
JOIN items on postID = items.id
I joined the items table on the wrong id :)
Related
I have the following query in my application:
SELECT
p.old_product_id,
l.product_id,
p.sku,
p.title,
p.option_one,
p.option_two,
FROM
lookup_id l
JOIN temp_price_tables_data p USING (sku);
And it works great. However, a small percentage of records from the temp_price_tables_data tables don't make it to the results.
This is because the skus from the lookup_id table don't exist in the temp_price_tables_data.
Is there a way to keep these records in the new data?
Or is there a way to only get those records so I can store the result for later processing?
EDIT:
First table columns = old_product_id, sku, title, option_one, option_two
Second table column = product_id, sku
Tables should have SKU in common.
Use a left outer join:
SELECT
*
FROM
lookup_id l
LEFT OUTER JOIN price_tables_data p on l.sku = p.sku
WHERE old_product_id IS NULL;
That will get you all the records that are in temp_price_tables_data but not in lookup_id
Query that displays all "Father (Husband)" names in alphabetical order from Guardian table with total number of their "Wives" (use nested query to find count of wives).
enter image description here
You could use a self join and group by
select a.guardianName as husband_name, count(*) as count_of_wives
from my_table a
left join my_table b on b.husband_id = a.guardianId
where a.husband_id is null
group by a.guardianName
I'm running some SQL through phpmyadmin. I'm trying to do a query with 2 tables. The first xlsws_product has all the product info the second xlsws_category_assn contains two columns one with category id and the other with product id. The category names are in a third table but I don't need them for this.
What I am looking to do is select the rows from the product table that are in the categories with id 210, 218 and 370. This is what I tried so far:
SELECT *
FROM xlsws_product
JOIN xlsws_product_category_assn ON xlsws_product.id = xlsws_product_category_assn.product_id
WHERE
xlsws_product_category_assn.category_id = '210' OR '218' OR '370'`
The result for this gave me 24090 rows from a bunch of categories and there should only by a handful of rows in those categories. What's bizarre here is that there are 56474 rows in the product table so I'm not sure how the results are being filtered.
Just for the hell of it I tried limiting my query to just one category id with the following query:
SELECT *
FROM xlsws_product
JOIN xlsws_product_category_assn ON xlsws_product.id = xlsws_product_category_assn.product_id
WHERE xlsws_product_category_assn.category_id = '210'
This yielded zero rows...
I'm sure there is something simple I am missing but after spending a while searching for a solution I just can't figure it out. Thanks for the help.
If you need to find all the data which are in the category '210' OR '218' OR '370'
You can do as
SELECT * FROM
xlsws_product xp
JOIN xlsws_product_category_assn xpc ON xp.id = xpc.product_id
WHERE
xpc.category_id in (210,218,370)
If you need to find the products which have all the 3 given category you can do as
SELECT * FROM
xlsws_product xp
JOIN xlsws_product_category_assn xpc ON xp.id = xpc.product_id
WHERE
xpc.category_id in (210,218,370)
group by xp.id having count(*) = 3
I have three tables that I need get information from, 1 table has the information in and the other two hold information that i need to count.
so the first tables structure is:
tbl_img
img_id
img_name
tbl_comments
comment_id
img_id
comment
tbl_vote
vote_id
logo_id
I want the results to have the count of comments and votes that relate to each logo.
I have a bit of the query which is for the count of comments, but have no idea for the syntax for the second join.
SELECT l.img_id, l.img_name, COUNT(c.comment_id) AS comment_count
FROM tbl_images as l
LEFT OUTER JOIN tbl_comments AS c USING (img_id);
Can anyone help?
how about this :
SELECT l.img_id, l.img_name,
(SELECT COUNT(*) FROM tbl_comments c WHERE i.img_id = c.img_id ) AS comment_count,
(SELECT COUNT(*) FROM tbl_vote v WHERE i.img_id = v.img_id ) AS vote_count
FROM tbl_images i
Sounds like you need two queries for this: One for counting the votes, and one for counting the comments.
As far as I know, COUNT counts result rows, and joins create result rows to display all allowed permutations of joined tables.
Assuming you have I entries, each with J comments and K votes, you would receive J*K rows for each entry after joins, and COUNTs would both return that J*K instead of the correct amount.
I do not remember if you can do inner queries in MySQL, but that would be the way to go.
(See #Kevin Burtons answer)
I have 2 tables:
category(inter_archi_cat table) and their linking with other
entities(inter_archi table )
I want to select only categories which linked to any entity.
SELECT *
FROM inter_archi_cat
WHERE id IN (SELECT GROUP_CONCAT(DISTINCT sub_cat) as allcat
FROM inter_archi)
If I ran the subquery individually, I'm giving correct results (apx 40 records). But while running with main query its giving only 1 record.
I have done it:
here is changed query:
SELECT distinct cat.id,cat.name
FROM inter_archi_cat as cat
join inter_archi as inter on (cat.id in (inter.sub_cat))
got idea from
MySQL - How to use subquery into IN statement by value
You don't have to use IN and subquery. You can achieve the same result using simple inner join:
SELECT ic.* FROM inter_archi_cat ic
JOIN inter_archi i
ON i.sub_cat = ic.id
GROUP BY ic.id
which would be a lot faster.