Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have table :
table a:
id | content
1 | a, b, c
2 | b
3 | b, c, d
Table b:
id | name | value
1 | a | 3000
2 | b | 4505
3 | c | 1257
4 | k | 2323
i want output like this :
id | value | content
1 | 3000, 4505, 1257 | a, b, c
2 | 4505 | b
3 | 1257 | c
You may try joining the two tables with the help of FIND_IN_SET. Then aggregate by the b id to generate the CSV values:
SELECT
a.id,
GROUP_CONCAT(b.value ORDER BY b.id) value,
GROUP_CONCAT(b.name ORDER BY b.id) content
FROM TableA a
INNER JOIN TableB b
ON FIND_IN_SET(b.name, a.content) > 0
GROUP BY
a.id;
Note: You should generally avoid storing CSV data in your SQL tables. Having the need to use FIND_IN_SET is usually a data smell.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was trying to fetch data from following tables.
table_question
q_id | question
1 | q1
2 | q2
3 | q3
4 | q4
table_answer
a_id| answer
1 | a1
2 | a2
3 | a3
4 | a4
5 | a5
table_entity
e_id| q_id | a_id
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 4
5 | 3 | 5
6 | 4 | 2
I want a question and answer. Please give me some suggestion.
Fetche data by using doctorine 2 ORM.
Try this mysql with your phpmyadmin
$qry = "SELECT quetion.que,answer.ans FROM entity INNER JOIN quetion ON quetion.id=entity.q_id Inner Join answer On answer.id=entity.a_id";
There are use join table for join those table and get the data from those table
you need to use join query. With joining both table you can get all tables relational data
$query = SELECT table_question.question,table_answer.answer FROM table_entity INNER JOIN table_question ON table_question.id=table_entity.`q_id ` Inner Join table_answer On table_answer.id=table_entity.a_id ;
Here you go:
SELECT table_question.question, table_answer,answer FROM table_entity
JOIN table_question ON (table_entity.q_id = table_question.q_id)
JOIN table_answer ON (table_entity.a_id = table_answer.a_id)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i have 3 tables which i want to combine in a empty table,
Table A contains:
a_id | name
1 | john
2 | mic
3 | rog
and
Table B contains:
b_id | name
10 | rims
11 | sara
and
Table c contains:
c_id | name
20 | johny
21 | sun
22 | rose
23 | pash
24 | ed
25 | ese
and i have one empty table D, which will have id's of all three above tables:
Table D columns are;
a_id | b_id | c_id
how can i insert all id's in table D? and
when i run query.
Select*from table_D
it should show all id's from table(a,b,c).
Your question is rather vague, because you don't specify what d looks like. Let me assume that you was a Cartesian product of all ids. This seems like a reasonable assumption. Then:
insert into d (a_id, b_id, c_id)
select a.a_id, b.b_id, c.c_id
from a cross join b cross join c;
Here is a rextester demonstrating it.
Here's what you ask for:
INSERT d
SELECT a.id, b.id, c.id
FROM a CROSS JOIN b CROSS JOIN c
Though I doubt it's what you want!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Please I need some help in writing an SQL statement for a PHP application that involves four tables. Here is the situation.
Each Art in Table B has a list of categories in Table D
Each PlaceID in Table A has a list of categories in Table C
I need to select Art from Table B to fill a PlaceID in Table A, but both Art and PlaceID must have at least one category in common.
I will appreciate any help that I can get.
TABLE A
PlaceID |
1 |
2 |
TABLE B
ArtID | Art
1 | Art1
2 | Art2
3 | Art3
TABLE C
ID | PlaceID | Category
1 | 1 | Cat1
2 | 1 | Cat2
3 | 2 | Cat3
4 | 2 | Cat1
5 | 3 | Cat1
TABLE D
TabID | ArtID | Category
1 | 1 | Cat1
2 | 1 | Cat2
3 | 1 | Cat3
4 | 2 | Cat1
5 | 2 | Cat2
For a given myPlaceID you can find possible Art values as follows:
SELECT DISTINCT B.Art
FROM B
INNER JOIN D
ON D.ArtID = B.ArtID
INNER JOIN C
ON C.Category = D.Category
WHERE C.PlaceID = :myPlaceID;
You don't need table A in this query.
Here is a fiddle. Note that for the existing values for PlaceID (1, 2, 3) you will always get 2 records as result (Art1, Art2) with the example data you provided. So you might want to use some other data to get more variation in your results.
Assuming the two tables tablec and tabled are related with Category, then you can do this:
SELECT
b.Art,
a.PlaceId,
COUNT(DISTINCT c.Category) AS TotalCategories
FROM tableA AS a
INNER JOIN tablec AS c ON a.placeID = c.PlaceID
INNER JOIN tabled AS d ON d.Category = c.Category
INNER JOIN tableB AS b ON b.ArtID = d.ArtID
GROUP BY b.Art, a.PlaceId
HAVING COUNT(DISTINCT c.Category) > 0;
This will give you only the places and arts that has at least one category in common, with count of categories in common.
SQL Fiddle Demo
This will give you:
| Art | PlaceID | TotalCategories |
|------|---------|-----------------|
| Art1 | 1 | 2 |
| Art1 | 2 | 2 |
| Art2 | 1 | 2 |
| Art2 | 2 | 1 |
As you can see this returned only art 1, 2 with places 1, 2 as they are the only arts and places have more than one category in common.
Side note: In your table designs, you are missing the many to many junction table between arts and places. So, iF the category is just to relate tablec with tabled, then you can get rid of either tablec or tabled, So that you will have both placeid and artid in the same table that will act as many to many junction table.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Database 1 : sandbox
table 1 : coordinates
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
database 2 : phone
table 2 : workstations
I only have READ privilege
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
PROBLEM:
I only have READ privilege on that second table of that database.
I want to join both tables so I can display the "ws" field on screen for my PHP script.
My "station_number" field values are written differently from the "pod" field
(they have a zero in front of the letter if it is a single digit after the letter) does it make a difference?
I've seen examples online on how to join but for some reason I can't get it to work.
Do I need to create an extra field on my main Table to store the field values from "ws" or what? I'm a bit confused.
Thanks in advance!
Yes, it makes a difference if there's a 0 in one and not in the other. You'll have to modify the value of one or the other. Something like SUBSTR(station_number, 0, 1) + LPAD(SUBSTR(station_number, 2), 2, '0').
And to join the tables from different databases, you just have to put the database name in front of the table name. Probably something like sandbox.coordinates.
So your query might look something like this:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON (SUBSTR(c.station_number, 0, 1) + LPAD(SUBSTR(c.station_number, 2), 2, '0')) = w.pod
If you can update one of your tables to make the columns match, then your query is as simple as:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON c.station_number = w.pod
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I think this is a really simple question but I can't crack it.
I have two tables in my mysql database, clubs_db and leagues_db.
clubs_db
id | name
1 | Club1
2 | Club2
3 | Club3
4 | Club4
5 | Club5
6 | Club6
leagues_db
id | team1 | team2 | team3 | team1_name | team2_name | team3_name |
1 | 1 | 2 | 3 | | | |
2 | 4 | 5 | 6 | | | |
All I want to do is insert the relevant club name into leagues_db from clubs_db.
I also want this to happen automatically when the values in leagues_db change.
Thanks if anybody can help me.
It sounds like you would be better served by dropping the teamN_name columns and using a view that joins the two tables together:
CREATE VIEW leagues_with_names AS
SELECT
l.id, l.team1, l.team2, l.team3,
t1.name AS team1_name,
t2.name AS team2_name,
t3.name AS team3_name
FROM leagues_db l
LEFT OUTER JOIN clubs_db t1 ON l.team1 = t1.id
LEFT OUTER JOIN clubs_db t2 ON l.team2 = t2.id
LEFT OUTER JOIN clubs_db t3 ON l.team3 = t3.id;
Then you can SELECT ... FROM leagues_with_names and not have to worry about the details of the join. Note that the view is not a table in itself; it will fetch data from the other two tables automatically. This means that it will be always up to date.
(See a demo of this query.)