I have two tables:
Table1:
- id
- name
- table2_id1
- table2_id2
- table2_id3
Table2:
- id
- name
Table1:
id | name | table2_id1 | table2_id2 | table2_id3
1 | blabla | 1 | 2 | 3
2 | blabla2 | 2 | 3 | 1
Table2:
id | name
1 | aaa
2 | bbb
3 | ccc
I would like to display a name from Table1 and many names from Table2 which are joined, example:
*blabla | aaa | bbb | ccc*
I hope you get what I mean.
EDIT:
I tried something like this:
SELECT Table1.name, Table2.name, Table2.name, Table2.name<BR>
FROM Table1 JOIN Table2 ON<BR>
You can use left join queries:
SELECT a.name as "name", b.name as "table2_id1_name", c.name as "table2_id2_name", d.name as "table2_id3_name"
FROM Table1 a
LEFT JOIN Table2 b ON (a.table2_id1 = b.id)
LEFT JOIN Table2 C ON (a.table2_id2 = c.id)
LEFT JOIN Table2 d ON (a.table2_id3= d.id)
Hope it work as you expect. :)
SELECT a.name, b.name, c.name, d.name
FROM Table1 a, Table2 b, Table2 c, Table2 d
WHERE a.table2_id1 = b.id
AND a.table2_id2 = c.id
AND a.table2_id3= d.id
Related
there are two tables
a
------------------------------
id | Name
------------------------------
1 | Alpha
-----------------------------
2 | Beta
-----------------------------
3 | Gamma
-----------------------------
4 | Delta
-----------------------------
and another table b with foreign key of table a
b
-----------------------------
id | a_id | Film
-----------------------------
1 | 1 | Bladerunner
-----------------------------
2 | 1 | Star Wars
-----------------------------
3 | 3 | Superman
-----------------------------
4 | 4 | Rollerball
-----------------------------
Write an SQL query using outer join to get all names from table āaā that don't have a film starting with āSā.
Query result should be:
Beta
--------
Delta
Use correlated subquery with not exists
DEMO
SELECT *
FROM tablea a
LEFT JOIN tableb b
ON a.id = b.a_id
WHERE NOT EXISTS (SELECT 1
FROM tableb b1
WHERE b.a_id = b1.a_id
AND film LIKE 'S%')
OR you can use below query to avoid subquery
SELECT NAME,
Sum(CASE
WHEN film LIKE 'S%' THEN 1
ELSE 0
END)
FROM t1 a
LEFT JOIN t2 b
ON a.id = b.a_id
GROUP BY NAME
HAVING Sum(CASE
WHEN film LIKE 'S%' THEN 1
ELSE 0
END) = 0
OUTPUT:
name
Delta
Beta
SELECT a.NAME
FROM a
LEFT JOIN b
ON a.id = b.a_id
WHERE a.NAME NOT IN (SELECT a.NAME
FROM a
LEFT JOIN b
ON a.id = b.a_id
WHERE b.film LIKE 's%')
GROUP BY a.NAME
output
Beta
Delta
according to the OPs question which he has not mentioned and just gave output part in his question... he wants all the name of the table a where film name do not start with letter S from table b. the above query fullfilled the requirements of OPs completely and in most standard way.
OP just mentioned the join in the question but he do not mentioned that he want those id name also which do not exist in table b in a_id i.e the output part beta.
use substring and join
select a.name from t1 a left join
t2 b on a.id=b.a_id
where a.id not in ( select b.a_id from t2 b
where
upper(substring(b.Film,1,1)) like '%S%' and b.a_id is not null
)
output
name
Delta
Beta
demo link
I want to count the number of articles (expected: 2) after grouping by an id but it currently returns only rows with 1 since it counts the occurrences after the GROUP BY.
article:
+-------+---------+
| id | name |
+-------+---------+
| 1 | Apple |
| 2 | Orange |
| 3 | Peaches|
+-------+---------+
article_category:
+---------------+----------------+
| article_id | category_id |
+---------------+----------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
+---------------+----------------+
Can this be done without a subquery and without using SQL_CALC_FOUND_ROWS? (I'm pretty restricted as I'm using an API endpoint). My only other idea is to simply retrieve all ids and then count them with PHP.
This is my current query:
SELECT COUNT(DISTINCT a.id) as number_articles
FROM article a
INNER JOIN article_category c ON a.id = c.article_id
WHERE c.category_id IN (1, 2, 3)
GROUP BY a.id;
Desired output:
+--------------------+
| number_articles |
+--------------------+
| 2 |
+--------------------+
Here is an SQLFiddle as suggested in the comments: http://sqlfiddle.com/#!9/8b2975/1
Based on the expected result you added now. You can remove the GroupBy, its really not achieving much here, this should work:
select count(distinct a.id)
from article a
INNER JOIN article_category c ON a.id = c.article_id
WHERE c.category_id IN (1, 2, 3)
try below solution.
select count(DISTINCT a.id) as total
from article a
INNER join article_category c on a.id = c.article_id
WHERE category_id in (1,2,3);
RESULT:
total
2
Just count the value of c.category_id instead of a.id
SELECT COUNT(c.category_id) FROM article a INNER JOIN
article_category c ON a.id = c.article_id WHERE c.category_id
IN (1, 2, 3) GROUP BY a.id;
SELECT COUNT(DISTINCT a.id)
FROM article a
INNER JOIN article_category c ON a.id = c.article_id
I have three mysql tables, category,students and student_category. for each student there is 1 or more category will be there and it is stored in student_category as follows.
1) Categgory
----------------------------
id | category_name
---------------------------
1 | A
2 | B
3 | C
4 | D
2) Students
--------------------------
id | name
--------------------------
1 | John
2 | Kumar
3 | Ashok
4 | Jorge
5 | Suku
-------------------------
2) student_category
-----------------------------------------
id | student_id | category_id
-----------------------------------------
1 | 1 | 2
2 | 1 | 4
3 | 2 | 3
4 | 2 | 1
5 | 3 | 2
------------------------------------------
I need to select students which contain category_id 2 and 4.
i used query as follows but it return either students contain category 2 or category 4.
select A.name from students A, student_category B where A.id=B.student_id
and B.category_id IN (2,4)
Try this query:
SELECT t1.id,
t3.name
FROM students t1
INNER JOIN student_category t2
ON t1.id = t2.student_id
INNER JOIN students t3
ON t1.id = t3.id
WHERE t2.category_id IN (2, 4)
GROUP BY t1.id
HAVING COUNT(DISTINCT t2.category_id) = 2
Explanation:
This query joins together the students and student_category tables, and then removes all records which are not category 2 or 4. This means that each student would then only have category 2 and 4 records associated with him. The HAVING clause then restricts further by requiring that a student have two distinct categories, which if true must mean that the student has both category 2 and 4.
Demo here:
SQLFiddle
try this :
select name from Students where id in (select student_id from student_category where category_id in (2,4))
your query is fine btw.
Try this one:
select
s.name
from
Students s,
Categgory c,
student_category sc
where
sc.student_id = s.id
and sc.category_id = c.id
and c.id = 2
and c.id = 4
You can check it on SQL Fiddle.
Have to take distinct student name as it will repeat if a student falls in more than one category.
I have Joomla tables (shown bellow) with some records in it (records are about companies). What I want to achieve is to get all records and group them to new table (I am using PHP for this). Problem is that there is 548 records (companies) and I got 439 records (after grouping).
Does someone knows where's the problem?
Here is database:
And here is query:
SELECT a.itemid as ID, a.title as Name, c.name as Categories, d.data_txt as Description, d.fieldid as FieldID FROM `jos_sobi2_item` as a
INNER JOIN `jos_sobi2_cat_items_relations` as b ON b.itemid=a.itemid
INNER JOIN `jos_sobi2_categories` as c ON c.catid = b.catid
INNER JOIN `jos_sobi2_fields_data`as d ON a.itemid=d.itemid
The issue is inner join is filtering out the records which are not in the related tables but in jos_sobi2_item
You may want to do left join something as
select
a.itemid as ID,
a.title as Name,
c.name as Categories,
d.data_txt as Description,
d.fieldid as FieldID
FROM `jos_sobi2_item` as a
left join `jos_sobi2_cat_items_relations` as b ON b.itemid=a.itemid
left join `jos_sobi2_categories` as c ON c.catid = b.catid
left join `jos_sobi2_fields_data`as d ON a.itemid=d.itemid
Here is an illustration of this
create table table1 (t1id int , name varchar(100));
insert into table1 values (1,'aa'),(2,'cc'),(3,'dd'),(4,'ee'),(5,'ff'),(6,'bb'),(7,'gg');
create table table2 (t2id int, description varchar(100));
insert into table2 values (1,'desc1'),(2,'desc2'),(3,'desc3');
create table table3 (t3id int, t1id int , t2id int);
insert into table3 values (1,1,2),(2,3,2),(3,2,2),(4,7,3);
create table table4 (t4id int , t1id int ,fieldid int);
insert into table4 values (1,1,10),(2,3,23),(3,4,34),(4,5,50);
select
t1.t1id,
t1.name,
t2.description,
t4.fieldid
from table1 t1
left join table3 t3 on t3.t1id = t1.t1id
left join table2 t2 on t2.t2id = t3.t2id
left join table4 t4 on t4.t1id = t1.t1id;
+------+------+-------------+---------+
| t1id | name | description | fieldid |
+------+------+-------------+---------+
| 1 | aa | desc2 | 10 |
| 2 | cc | desc2 | NULL |
| 3 | dd | desc2 | 23 |
| 4 | ee | NULL | 34 |
| 5 | ff | NULL | 50 |
| 6 | bb | NULL | NULL |
| 7 | gg | desc3 | NULL |
+------+------+-------------+---------+
The above example is similar to what you have.
If you still want to use INNER
1. Try a LEFT JOIN instead a INNER JOIN, and put a condition in WHERE
SELECT a.itemid as ID, a.title as Name, c.name as Categories, d.data_txt as Description, d.fieldid as FieldID FROM `jos_sobi2_item` as a
LEFT JOIN `jos_sobi2_cat_items_relations` as b ON b.itemid=a.itemid
LEFT JOIN `jos_sobi2_categories` as c ON c.catid = b.catid
LEFT JOIN `jos_sobi2_fields_data`as d ON a.itemid=d.itemid
WHERE b.itemid IS NULL OR c.catid IS NULL OR d.itemid IS NULL
Look at your results and see where you have nulls, after that you will discover what records from the jos_sobi2_item don't have a connection in the other tables.
Or if you want to use LEFT JOIN :
1.Run the query:
SELECT a.itemid as ID, a.title as Name, c.name as Categories, d.data_txt as Description, d.fieldid as FieldID,
COUNT(b.itemid), COUNT(c.catid), COUNT(d.itemid)
FROM `jos_sobi2_item` as a
LEFT JOIN `jos_sobi2_cat_items_relations` as b ON b.itemid=a.itemid
LEFT JOIN `jos_sobi2_categories` as c ON c.catid = b.catid
LEFT JOIN `jos_sobi2_fields_data`as d ON a.itemid=d.itemid
GROUP BY a.itemid
Analyze the counts for duplicate records, you can put a condition where count > 1 on every count.
1) I have two table:
+------ +---------
topic mytask
+--------- +---------
ID | What ID | TopicID
---------- ------------
1 | A 1 | 1
2 | B
2) I have SQL as below:
SELECT
a.id, b.id
FROM
topic a
JOIN (
SELECT
b.id,b.topicid
FROM
mytask b
) b on (b.topicid=a.id)
3) I have output as below (1 ROW ONLY):
ID | ID
-------
1 | 1
My expected output is as below (2 ROW, PRIORITY TO TOPIC BY LISTING THEM ALL) :
a.ID | b.ID
-------------
1 | MATCHED - OK - TAKE IT!!!
2 | NULL or what,ever...
How can i do that?
Use a left outer join:
SELECT
a.id, b.id
FROM
topic a
LEFT JOIN (
SELECT
b.id,b.topicid
FROM
mytask b
) b on (b.topicid=a.id)
and simplify the query to
SELECT
a.id, b.id
FROM
topic a
LEFT JOIN
mytask b ON b.topicid = a.id