If I have this three tables:
table1: id, title, content
connection: id_t1, id_t2
table2: id, title, content
In my case I just select a single row of table1. For this result there are many rows in table2. The connection of both tables can be found in the table 'connection'
How do I have to create the query to get this result?
table1-title
table2-content1
table2-content2
table2-content3
table1-content
If I understand correctly I believe you want to use GROUP BY with the GROUP_CONCAT function
The query would look something like this:
SELECT table1.title, GROUP_CONCAT(table2.content) as table2.group_content, table1.content
FROM table1
JOIN connection on table1.id = id_t1
JOIN table2 on connection.id_t2 = table2.id
GROUP BY table2.content
This would give you one row for each table1.id, with multiple table2.content rows concatenated into one column (called table2.group_content in this example).
select title from table1 where title_id = 1
UNION
select t2.content
from table2 t2, table1 t1, connection c
where t1.title_id = 1
and t1.title_id = c.id_t1
and c.id_t2 = t2.title_id
UNION
select content from table1 where title_id = 1
Related
I have a table with two columns that are foreign keys (think in userID(int) : orderID(int) for example), and I have to know if orders 2, 3, 4, 5 exists for a user ID, in a where clause of a big query.
I need to optimize my database
SELECT table1.myrow FROM table1, table2
WHERE table1.myrow = table2.myrow
AND 1 IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)
AND 2 IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)
AND 3 IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)
I want to do something like this:
AND (SELECT * from mytable) IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)
How can I determine if my multiple value list exists for an ID? the rows i'm requesting are relations that consist in only a table with two foreign keys.
My relations:
Need to know if CONVOCATORIA_SECTOR have X relations for a CONVOCATORIAS(id_bdns_Conv)
You can do it by grouping by userid and a condition in the HAVING clause:
select userid
from tablename
where orderid in (2,3,4,5)
group by userid
having count(distinct orderid) = 4
This will select all the userids for which there exist the orderids 2, 3, 4 and 5.
as an ugly answer, you can concatenate both columns like this
AND (SELECT column1||column2 from mytable) IN (SELECT column1||column2 from table2 WHERE table2.id = table1.myrow)
is not the best way but it works
I have 3 tables like this
With the tables filled like this:
How do I search the cases In where on the table 3 the idtable 1 has all the id from table 2 related?
For example idtable1 = 1 would be an output of that query cuz is related with every id from idtable2
Presumably, you intend:
select t1.*
from table1 t1
where (select count(*) from table3 t3 where t3.idtable1 = t1.idtable1) =
(select count(*) from table2);
This shows all records from table1 where table3 contains all values of idtable2 -- assuming no duplicates in table3 (and that the ids are unique).
I have been trying to find the correct query for this problem but it doesn't quite work so I'm asking here:
I have 2 tables:
Table-1 has 5 rows with 6 attributes each
Table-2 has 3 rows with 5 attributes and EACH attribute corresponds to 1 row from table 1. What I want is a query that will give me all the attributes from table 1 that are contained in table 2. I've come up to this:
SELECT *
FROM Table1
WHERE PrimKey IN
(SELECT *
FROM Table2
WHERE PrimKey = Index)
However it won't let me do this because it says that on the second SELECT I can't select all but I have to choose. This way I can only view 1 row of Table1 stuff from Table2 but I want to view all of Table2's attributes.
SELECT *
FROM Table1
WHERE PrimKey IN
(SELECT PrimKey
FROM Table2)
Or INNER JOIN
SELECT t1.*
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.ReferencingColumn = t2.ReferencingColumn
I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.
I have two tables a and b which has a field name in it.
I need to list the data from these two tables. I thought of using union but in the result list data from the first table appears and then followed by the second.
what i want is to order by the field name so the result should be a mixed up of two tables in the order of name that is order by name.
select slug, name, 1 as mt
from tablea
union
select slug, name, 0 as mt
from tableb
order
by name;
The above is working well for me. will there be any complications in the result of this?
Suppose your query is
SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1
u can make it a subquery like this
SELECT * FROM (SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1) AS `result` ORDER BY `result`.`field1`
Or, you could use a Join query such as:
SELECT tablea.firstname, tablea.middlename, tablea.lastname, tableb.phone
FROM tablea, tableb
WHERE tablea.ID = tableb.ID
Then, you could sort the result however you like.