I have a query:
SELECT DISTINCT *
FROM table1 AS s
LEFT OUTER JOIN table2 AS t
ON s.s_id = t.t_id
WHERE (
s.body LIKE '%string%'
OR t.name LIKE '%string%'
)
ORDER BY s.time DESC
but I am still getting duplicate tuples. Why is this?
GROUP BY s.s_id
was the solution.
The result doesn't contain absolutely equal rows here so technically they aren't duplicated
To get rid of duplicates, you need to SELECT DISTINCT or GROUP BY only fields you need non-duplicated and outer join the rest data in subquery on the corresponding key values, taking only 1 (first or last or whatever) row from them.
Related
Why I get #1060 - Duplicate column name 'id'
SELECT COUNT(*) FROM (SELECT * FROM `tips` `t` LEFT JOIN
tip_usage ON tip_usage.tip_id=t.id GROUP BY t.id) sq
Probably because the * in select * selects two columns with the same name from tip_usage and tips.
Probably it's because the inner select yields two columns with the name id. Since you are not using those columns, you can just change the select to:
SELECT COUNT(*) FROM (SELECT t.id FROM `tips` `t`
LEFT JOIN tip_usage ON tip_usage.tip_id=t.id
GROUP BY t.id) sq
Your query is equivalent to this:
SELECT COUNT(DISTINCT id)
FROM tips
, there is no need in a join.
Are you sure you didn't want an INNER JOIN instead?
Had the same problem, renaming into select clause saved me
SELECT people.id, vehicle.id ...
I renamed it with AS keyword
SELECT people.id AS person_id, vehicle.id ...
I am struggling with a MySQL query which I cant get to work as I want.
In table1 I have co_id, name, code, product, logindate.
in table2 I have pr_id, productname, productno, price.
I want to count and group the PRODUCT from table1, so I can see how many that have picked for example product 1,2,3 etc.
But when I list the result on the page I will need productname, and productno for each id number in the GROUP search. table1.product is joined with table2.pr_id
This is what I have so far, but I think I am missing something with INNER JOIN or similar, right?
SELECT
codes.pickedgift,
products.productno,
products.productname,
COUNT(codes.pickedgift) as num
FROM
codes,
products
GROUP BY codes.pickedgift
ORDER BY codes.pickedgift
you missing the join condition, when you join 2 tables you should link primary key in table1 to its foreign key in another table, so your query can be:
SELECT
codes.pickedgift,
products.productno,
products.productname,
COUNT(codes.pickedgift) as num
FROM
codes INNER JOIN products ON codes.product = products.pr_id
GROUP BY codes.pickedgift
ORDER BY codes.pickedgift
You should use a sub-select for this query.
-- assuming I have your table structure correct.
SELECT p.productno, p.productname, num
FROM (SELECT codes.pickedgift, COUNT(codes.pickedgift) as num
FROM codes
GROUP BY codes.pickedgift) g
JOIN products p ON p.id = g.pickedgift
ORDER BY g.pickedgift
The other thing you have to make sure of is if you're using a group-by, the fields in your select must either be the fields in the group by, or aggregates. MySQL let's you include columns that are not part of the group-by / aggregate, it becomes ambiguous as to which value productno and productname should be represented, which is why I opted for a sub-select instead.
I have a query, a generalized version of which I've reproduced below:
SELECT TT.column
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date
I want to take the output of this query -- a single column output with multiple rows sorted by date -- and group concat it in another query as a derived table:
SELECT
T.column2,
GROUP_CONCAT(
SELECT TT.column
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date) AS concat_output
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date
However, this returns an error at the line of the GROUP_CONCAT command.
Thoughts on how to make this work?
EDIT: To give some more detail on why I wanted the derived table to work:
At the moment, without using GROUP_CONCAT, I get multiple rows that look like
a
a
b
b
a
a
c
c
d
a
If I try to GROUP_CONCAT as described by Mukesh's answer, using DISTINCT I get the following, for example, as a row: a, b, c, d when really I want a,b,a,c,d,a.
Thoughts?
Try this query
SELECT
T.column2,
GROUP_CONCAT(
DISTINCT TT.column
) AS concat_output
FROM Table1 TT
JOIN Table2 T USING (PRIMARYKEY)
GROUP BY T.Date
More detail to refer this link
http://www.mysqltutorial.org/mysql-group_concat/
What is the correct way to write this query for mysql (this one seems to work, but seems idiotic) (191 is hard code for a variable)
select t1.item_id, t1.item_name, t1.item_desc, t.quantity, t.price
from (select * from items i where i.item_id = 191) as T1
LEFT JOIN (select * from item_properties ip) as T
on t1.item_id = t.fk_item_id and t1.item_id=191;
T1.item_id is PK, T.fk_item_id is foreign key (? -- only can exist if parent T1.item_id exists)
This was my way of returning t.values as null when they don't exist (and can't be joined).
Thanks
SELECT
i.item_id, i.item_name, i.idem_desc,
p.quantity, p.price
FROM items i
LEFT JOIN item_properties p ON i.item_id = p.fk_item_id
WHERE i.item_id = 191
Left join is required so rows from left table are always returned, even when there are no matching row in the right table. But those SELECT in the FROM were not necessary. Try to keep things simple when they are.
if you want to read how to implement joins correctly in mysql, read this
and if you want result only if matches in both tables use inner join for FK and PK
If a left outer join provides a value (t2.id for my example), I would like to select it. Note that for my particular data, the left join will only have one or zero matches. If not, I would like to select another value.
The following will not work because my query without the LIMIT 1 might return multiple rows and the first might happen not to be one where the left join matched.
SELECT COALESCE(t2.id,t1.id) AS id
FROM t1
LEFT OUTER JOIN t2 ON t2.id=t1.t2_id
WHERE t2.fk=123
ORDER BY id DESC LIMIT 1
How can I select the value returned by a left join where a limit exists?
Change the order by to:
order by (t2.id is not null) desc