Get only rows where exists in table? - mysql

I need to select all rows from table: Announcements where there are exist rows in table Categories_announcements for user by condition:
Categories_user.category_id = Categories_announcements.category_id
I tried SQL query, see link
It should returns me onlu one row Announcements.id = 1, because user has only one category in Categories_user.category_id.
EDIT:
I have tested SQL that you shared, so this is query:
select *
from `announcements`
where exists (
select 1
from `announcement_category`
inner join `user_category` on `user_category`.`category_id` = `announcement_category`.`category_id`
where `Auser_category`.`user_id` = 1
and `announcement_category`.`announcement_id` = announcements.id
)
It returns me an error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') LIMIT 0, 25' at line 7

I don't think you need an EXISTS subquery. A basic join statement should work.
select a.*
from Categories_user cu
join Categories_announcements ca on ca.category_id = cu.category_id
join Announcements a on a.id = ca.announcement_id
where cu.user_id = 1

You need to correlate the subquery with the Announcements table:
select *
from Announcements a
where exists (
select 1
from Categories_announcements ca
inner join Categories_user cu on cu.category_id = ca.category_id
where cu.user_id = 1
and ca.announcement_id = a.id
)
Demo

You are close. Try this:
select a.*
from Announcements a
where exists (select 1
from Categories_announcements ca join
Categories_user cu
on cu.category_id = ca.category_id
where ca.announcement_id = a.id and
cu.user_id = 1
);
Notes:
The key thing you are missing is the correlation clause to the outer query (ca.announcement_id = a.id).
The left join is superfluous. The where clause turns it into an inner join.
Table aliases make the query easier to write and to read.

You'r query is missing a relationship between the announcement table in the outer query to the condition in the inner one:
SELECT *
FROM announcements a
WHERE EXISTS (SELECT *
FROM categories_announcements ca
LEFT JOIN categories_user cu ON cu.category_id = ca.category_id
WHERE cu.user_id = 1 AND
a.id = ca.announcement_id -- Here!
)
SQLFiddle

Another possible workaround without a sub-query:
SELECT a.id AS accouncement_id, a.name AS annoucement_name
FROM Categories_user cu
INNER JOIN Categories_announcements ca
ON cu.category_id = ca.category_id
AND cu.user_id = 1
INNER JOIN Announcements a
ON ca.announcement_id = a.id;
Demo link

Related

Sql query within an inner join

i have this mysql statement :
SELECT ca.*, MAX(ca.id), v.*,a.submit_dt from callback_holding ca
inner join valuations v on v.Ref = ca.ref
inner join answer a on a.title = ca.ref
where v.Consultant = '$user' and ca.isholding = 2
GROUP BY ca.ref DESC order by ca.reccomendeddate asc
But the problem is if there is not an entry in "answer" then it doesn't show up in the list. What is the correct way to bring back everything and just "null" if there is nothing in the "answer" table?
Thanks
Your query has several problems. First, you are grouping by the ref column from the callback_holding table, but are selecting non aggregate columns not only from this table, but from other tables. To get around this, you should do the aggregation to find maximum IDs in callback_holding in a subquery, and then join it to the other tables.
Next, you mentioned that if no answer be found, you get back no records. This is the nature of an INNER JOIN, but if you switch the join to answer to use a LEFT JOIN, then no records up to that point in the query will be lost. Note that I used COALESCE(a.submit_dt, 'NA') to display NA in the event that this column from the answer table be NULL. If this column be datetime, then you should use a suitable default value, e.g. NOW().
SELECT ca.*,
v.*,
COALESCE(a.submit_dt, 'NA') AS submit_dt, -- display 'NA' if no answer
t.max_id
FROM callback_holding ca
INNER JOIN
(
SELECT ref, MAX(id) AS max_id
FROM callback_holding
GROUP BY ref
) t
ON t.ref = ca.ref AND
t.max_id = ca.id
INNER JOIN valuations v
ON v.Ref = ca.ref
LEFT JOIN answer a
ON a.title = ca.ref
WHERE v.Consultant = '$user' AND
ca.isholding = 2
ORDER BY ca.reccomendeddate
try with:
SELECT ca.*, MAX(ca.id), v.*,a.submit_dt from callback_holding ca
INNER join valuations v on v.Ref = ca.ref
LEFT join answer a on a.title = ca.ref
WHERE v.Consultant = '$user' and ca.isholding = 2
GROUP BY ca.ref DESC order by ca.reccomendeddate asc

Inner join 3 tables with only specific columns selected

I cannot solve this issue, I have tree tables, inner join and I'm after selecting only specific columns because I'm concerned about performance issue if I just leave SELECT * to do the job.
I have tables: ugovori-artikli, ugovori and ids, working SELECT * query:
SELECT * FROM `ugovori-artikli`
INNER JOIN `ugovori`
ON `ugovori-artikli`.`ugovor_id` = `ugovori`.`id`
INNER JOIN `ids`
ON `ugovori`.`kupac_id` = `ids`.`id`
AND `ugovori-artikli`.`artikal` = ?
and non-working query, my attempt:
SELECT a.*,
b.*,
c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b
ON `ugovori-artikli`.`ugovor_id` = `ugovori`.`id`
INNER JOIN `ids` AS c
ON `ugovori`.`kupac_id` = `ids`.`id`
AND `ugovori-artikli`.`artikal` = ?
I get an error:
Error in query (1054): Unknown column 'ugovori-artikli.ugovor_id' in 'on clause'
Use the alias name
SELECT a.*,
b.*,
c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b
ON a.`ugovor_id` = b.`id`
INNER JOIN `ids` AS c
ON b.`kupac_id` = c.`id`
AND a.`artikal` = ?
Hope this helps :)
You have to use the aliasses not the table name in your SQL statement.
SELECT a.*,b.*,c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b ON a.`ugovor_id` = b.`id`
INNER JOIN `ids` AS c
ON b.`kupac_id` = cid`
AND a.`artikal` = ?

Select Query based on values in result set

Is there anyway to select all from 1 table based on the result of one query which contains multiple values without having to do 2 separate queries?
Say long join query will produce a list of id's
SELECT xyz FROM table long join query WHERE id = array of ids found in result table
added example:
SELECT * FROM tweets as t
where t.user_id
in(
SELECT uff.id, uff.username
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)
The bit in the parenthesis returns an id and user name of who the user is following (uff.id=1)
How do i then get all 'tweets' by all the id's in the generated resultset
You can use subquery:
SELECT * FROM `table1` WHERE `id` IN (SELECT `table2`.id FROM `table2` )
You might want to check documentation for syntax
SELECT xyz FROM table_A join table_B WHERE table_A.id IN (SELECT ID FROM table_C);
I think you mean something like
edited after the OP edit
SELECT * FROM tweets as t
WHERE t.user_id
in(
SELECT uff.id //remove the second field, you just need the id
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)
After trying the in clause I coudnt get the results I was after but after rethinking what I was trying to do I got my results with an additional join clause
SELECT uff.username, t.content
FROM users as uf
JOIN followlinks as fl
ON uf.id = fl.user_id
JOIN users as uff
ON fl.follow_id = uff.id
JOIN tweets as t
ON t.user_id = uff.id
WHERE uf.id = 1

error mysql query with inner join

select simplex_comunes.cod_color_piel.descripcion as cod_color_piel, simplex_comunes.cod_sexo.descripcion as cod_sexo, count(*)
from simplex_comunes.cod_color_piel,simplex_comunes.cod_sexo
inner join simplex_ch.dat_trabajadores on simplex_ch.dat_trabajadores.id_color_piel = simplex_comunes.cod_color_piel.codigo
inner join simplex_ch.dat_trabajadores on simplex_comunes.cod_sexo.codigo = simplex_ch.dat_trabajadores.id_sexo
group by simplex_comunes.cod_color_piel.descripcion,simplex_comunes.cod_sexo.descripcion
the error is Not unique table/alias: 'dat_trabajadores',
Please help, thanks!!!
I haven't checked if your query is "intelligent" or not, but you have to use aliases as you use twice dat_trabajadores in your query.
You have to tell MySQL which table you use in your JOIN.
select simplex_comunes.cod_color_piel.descripcion as cod_color_piel, simplex_comunes.cod_sexo.descripcion as cod_sexo, count(*)
from simplex_comunes.cod_color_piel,simplex_comunes.cod_sexo
inner join simplex_ch.dat_trabajadores tr1 on simplex_ch.tr1.id_color_piel = simplex_comunes.cod_color_piel.codigo
inner join simplex_ch.dat_trabajadores tr2 on simplex_comunes.cod_sexo.codigo = simplex_ch.tr2.id_sexo
group by simplex_comunes.cod_color_piel.descripcion,simplex_comunes.cod_sexo.descripcion
you have two same aliases in your query.
rename them differently
select simplex_comunes.cod_color_piel.descripcion as descripcion,
simplex_comunes.cod_sexo.descripcion as cod_sexo_descripcion,
count(*)
from simplex_comunes.cod_color_piel,simplex_comunes.cod_sexo
inner join simplex_ch.dat_trabajadores t1 on t1.id_color_piel = simplex_comunes.cod_color_piel.codigo
inner join simplex_ch.dat_trabajadores t2 on simplex_comunes.cod_sexo.codigo = t2.id_sexo
group by simplex_comunes.cod_color_piel.descripcion,simplex_comunes.cod_sexo.descripcion

MySQL `INNER JOIN` multiples of the same table

Is it possible to INNER JOIN a MySQL query to achieve this result?
I have a table with Strategies and a table with Members. The Strategy table holds the ID of the author that corresponds to their ID in the Member table and the ID of an author that updated the existing author's work. Is it possible to grab a reference to both of these people at the same time? Something like the following, which returns no errors, but also no results...
SELECT * FROM Strategies
INNER JOIN Members AS a
INNER JOIN Members AS b
WHERE Strategies.ID='2'
AND Strategies.AuthorID = a.ID
AND Strategies.UpdateAuthorID = b.ID
Use a LEFT JOIN:
SELECT
s.*,
a.Name AS MemberName,
b.Name AS UpdatedMemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2 ;
If you want them in one column use COALESCE:
SELECT
s.*,
COALESCE(a.Name, b.Name) AS MemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
if domain is table name