This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
MySQL DISTINCT on a GROUP_CONCAT()
(6 answers)
Closed 4 years ago.
I want to create an advanced search based on one table and other tables
my tables:
estate :
+------+-------------+-------------------+-------------------+
| id | title | cat_id | date |
+------+-------------+-------------------+-------------------+
| 1 | test1 | 1 | 1526793203 |
| 2 | test2 | 2 | 1526793203 |
| 3 | test3 | 3 | 1526793203 |
+------+--------------+------------------+-------------------+
estate_risk
+------+-------------+----------------+--------------+
| id | estate_id | title | consequence |
+------+-------------+----------------+--------------+
| 3 | 1 | risktitle1 | 123 |
| 4 | 1 | risktitle2 | 433 |
| 5 | 1 | risktitle3 | 523 |
| 6 | 2 | risktitle4 | 976 |
| 7 | 2 | risktitle5 | 422 |
| 8 | 3 | risktitle6 | 124 |
+------+-------------+----------------+--------------+
related_estate
+------+-------------+----------------+--------------+
| id | estate_id | title | storage |
+------+-------------+----------------+--------------+
| 3 | 1 | testdata | 1 |
| 4 | 1 | testdata2 | 2 |
| 5 | 1 | testdata3 | 3 |
| 6 | 2 | testdata4 | 4 |
| 7 | 2 | testdata5 | 5 |
| 8 | 5 | testdata6 | 6 |
+------+-------------+----------------+---------------+
And some other tables...(foreign all tables is estate_id)
I want to get all the data in a row,in other words for example, I have several related data in the estate_risk table and when I use join query get this result data :
sample query one join:
SELECT R.id,R.title,C.title,C.storage FROM estate R LEFT JOIN estate_risk as C ON estate_risk .estate_id = R.id
result :
----------+------------------+----------------+--------------+
R.id | R.title | C.title | C.consequence
----------+------------------+----------------+--------------+
1 | test1 | risktitle1 | 123 |
1 | test1 | risktitle2 | 433 |
1 | test1 | risktitle3 | 523 |
2 | test2 | risktitle4 | 976 |
2 | test2 | risktitle5 | 422 |
3 | test3 | risktitle6 | 124 |
----------+------------------+----------------+--------------+
Everything is right but i want get all the data in a row that's mean only a row R.id with all C.title in one row
My main goal is to display the page search results just one estate with all other data tables
+------+-------------+----------------+----------------+
id | estate_title| estate_risk | related_estate |
+------+-------------+----------------+----------------+
1 test1 risktitle1 testdata1
risktitle2 testdata2
risktitle3 testdata3
---------------------------------------------------------
2 ...
Maybe my goal is not right, but I do not know how I can get this output And this should be done on the database side, or programming side ?
What ideas can be made on the programming side?
It looks like what you want is a GROUP_CONCAT to put together the risktitles and related_estates.
GROUP_CONCAT(C.title SEPARATOR '\n'),
GROUP_CONCAT(Some_other_column SEPARATOR '\n')
Once you have your group functions, I believe grouping by R.id as Yanet suggested and the data should come together correctly.
If you do have larger data sets though, there are limitations to MySQL's concatenation functions. So in that case you might need to return the data as you are already and then reformat it to match what you want in the PHP side.
Group Concat Limit
Related
I'm developing a PHP script, and I have the following table:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 1 | 1 | 2 | -B-A-C- |
| 2 | 1 | 3 | -B-C- |
| 3 | 2 | 4 | -C-B- |
| 4 | 3 | 1 | -A-B- |
| 5 | 1 | 6 | -A-C- |
| 6 | 2 | 5 | -C-B- |
| 7 | 2 | 7 | -B-C- |
| 8 | 3 | 8 | -A- |
+----+-----------+----------+--------------+
From this table I would like to select all the rows with "feature" LIKE "%-A-%", but displaying first the result with lowest "position", then all the rows that have same value for column "id_parent" of the first result, then row with the 2nd lowest "position" and all the rows that have same "id_parent" of the result with the 2nd lowest "position", and so on...
So the final result should be:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 4 | 3 | 1 | -A-B- |
| 8 | 3 | 8 | -A- |
| 1 | 1 | 2 | -B-A-C- |
| 5 | 1 | 6 | -A-C- |
+----+-----------+----------+--------------+
For some reason I can't explain here I need to have and HAVING clause for selecting the right "feature" value (...HAVING 'feature' LIKE '%-A-%' ...).
Is it possible to make all this with MySQL (possibly without subqueries) or by processing data results with PHP?
Does this help? I've left the last part of the problem as an exercise for the reader...
SELECT a.*
, c.*
FROM my_table a
JOIN
( SELECT id_parent, MIN(position) position FROM my_table WHERE feature = 'a' GROUP BY id_parent ) b
ON b.id_parent = a.id_parent
AND b.position = a.position
JOIN my_table c
ON c.feature = a.feature
AND c.id_parent = a.id_parent;
I have a table with concatenated values within both rows, I am therefore uniquely retrieve ranking for each row in the tables.
UPDATE
The other tables has been added to question
NamesTable
NID | Name |
1 | Mu |
2 | Ni |
3 | ices |
GroupTable
GID | GName |
1 | GroupA |
2 | GroupB |
3 | GroupC |
MainTable
| NID | Ages | Group |
| 1 | 84 | 1 |
| 2 | 64 | 1 |
| 3 | 78 | 1 |
| 1 | 63 | 2 |
| 2 | 25 | 2 |
| 3 | 87 | 2 |
| 1 | 43 | 3 |
| 2 | 62 | 3 |
| 3 | 37 | 3 |
Now the first Name is equated to the first age in the table, I am able to equate them using php and foreach statements, Now the problem is with the ranking of the ages per each group. I am ranking the names uniquely on each row or group.
Results which is expected
| Names | Ages | Group | Ranking |
| Mu,Ni,ices | 84,64,78 | 1 | 1,3,2 |
| Mu,Ni,ices | 63,25,87 | 2 | 2,3,1 |
| Mu,Ni,ices | 43,62,37 | 3 | 2,1,3 |
In my quest to solving this, I am using GROUP_CONCAT, and I have been able to come to this level in the below query
SELECT
GROUP_CONCAT(Names) NAMES,
GROUP_CONCAT(Ages) AGES,
GROUP_CONCAT(Group) GROUPS,
GROUP_CONCAT( FIND_IN_SET(Ages, (
SELECT
GROUP_CONCAT( Age ORDER BY Age DESC)
FROM (
SELECT
GROUP_CONCAT(Ages ORDER BY Ages DESC ) Age
FROM
`MainTable` s
GROUP by `Group`
) s
)
)) rank
FROM
`MainTable` c
GROUP by `Group`
This actually gives me the below results.
| Names | Ages | Group | Ranking |
| 1,2,3 | 84,64,78 | 1 | 7,9,8 |
| 1,2,3 | 63,25,87 | 2 | 5,6,4 |
| 1,2,3 | 43,62,37 | 3 | 2,1,3 |
The only problem is that the ranking Values increase from 1 to 9 instead of ranking each row uniquely. Its there any idea that can help me cross over and fix this? I would be grateful for your help. Thanks
This question already has answers here:
How to copy data from one table to another new table in MySQL?
(13 answers)
Closed 9 years ago.
How to copy mysql data from table one to table two [php] ?
I want to copy data from table one to table two WHERE type=1 , How can I do ?
Table one
| type | number | name | date |
| 1 | 1 | a | 12 |
| 1 | 2 | b | 13 |
| 2 | 6 | c | 14 |
| 1 | 8 | x | 17 |
| 2 | 8 | e | 19 |
| 3 | 6 | f | 11 |
| 2 | 4 | h | 10 |
| 1 | 7 | i | 11 |
| 1 | 9 | p | 13 |
| 2 | 5 | r | 17 |
| 1 | 3 | t | 12 |
table two (out put)
| number | name |
| 1 | a |
| 2 | b |
| 8 | x |
| 7 | i |
| 9 | p |
| 3 | t |
This is the basic standard query for the MySQL.
INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE WHERE Type = 1
Using MySQLi in PHP you can do like this..
$mysqli->query("INSERT INTO `TARGET_TABLE` SELECT * FROM `SOURCE_TABLE` WHERE `Type` = 1")
You can do this with one SQL statement:
insert into two(number, name)
select number, name
from one
where type = 1;
Would like to get the following as a result from the table structure below (MYSQL + PHP)
array[0][name]1,[desc]red,[title]hero,[desc]strong,[desc2]smells,[img][0]red1,[img][1]red2,[img][2]red3,ext[0].jpg,[ext][1].gif,[ext][2].png,[count][0]253,[count][1]211,[count][2]21,[count][3]121,[dist][0]5,[dist][1]5,[dist][2]12,[dist][3]2,[score][0]2,[score][1]3,[score][2]1,[score][3]5,[score][4]4,[val][0]5,[val][1]1,[val][2]4,[val][3]3,[val][4]4
The problem I have with a simple SELECT, JOIN and GROUP_CONCAT is that the values duplicate after selecting all the images.
I've tried various other ways for example selecting the data by row combined with a foreach loop in PHP, but I end up with lots of duplicates, and it looks very messy.
I also though about splitting it into multiple selects instead of using one, but I really would like to know if it can be done with one select.
Could someone help me with an MYSQL select? Thanks
game
+-----+----------+
| pid | name |
+-----+----------+
| 1 | red |
| 2 | green |
| 3 | blue |
+-----+----------+
detail
+-----+------+--------+-------+--------+
| id | pid | title | desc | desc 2 |
+-----+------+--------+-------+--------+
| 1 | 1 | hero |strong | smells |
| 2 | 2 | prince |nice | tall |
| 3 | 3 | dragon |big | green |
+-----+------+--------+-------+--------+
image
+-----+-----+-----+----+
| id | pid | img |ext |
+-----+-----+-----+----+
| 1 | 1 | red1|.jpg|
| 2 | 1 | red2|.gif|
| 3 | 1 | red3|.png|
+-----+-----+-----+----+
devmap
+-----+-----+-------+------+
| id | pid | count | dist |
+-----+-----+-------+------+
| 1 | 1 | 253 | 5 |
| 2 | 1 | 211 | 5 |
| 3 | 1 | 21 | 12 |
| 4 | 1 | 121 | 2 |
+-----+-----+-------+------+
stats
+-----+-----+-------+------+
| id | pid | scrore| val |
+-----+-----+-------+------+
| 1 | 1 | 2 | 5 |
| 2 | 1 | 3 | 1 |
| 3 | 1 | 1 | 4 |
| 4 | 1 | 5 | 3 |
| 5 | 1 | 4 | 3 |
+-----+-----+-------+------+
When you do a JOIN that involves more than a 1:1 mapping between tables you're going to have duplicate data, and there's no way to get around that in the query.
You can break it out into multiple selects, or you can loop through the result set and pare out whatever duplicate information you don't want.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
mysql with comma separated values
I have two tables with more lines, like this:
1. numberstable
---------------------------
| number_id | number_name |
---------------------------
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
.
.
.
---------------------------
2. testtable
-------------------------------
| test_id | numbers | sthelse |
-------------------------------
| 1 | 2.4.5.6 | text1 |
| 2 | 4.8.7.1 | text2 |
| 3 | 2.7.8.5 | text3 |
-------------------------------
First I would like to combine all three "numbers" rows from table "testtable" to get something like this: 1.2.4.5.6.7.8 and then exclude it in next query. This query is "SELECT number_id, number_name FROM numberstable ORDER BY number_name". After excluding I would like to show just numbers which aren't in use in "testtable" (9, 10, 11, ...).
How to do that?
If you are trying to relate the numberstable to the testable. I would think you would be much better served to add another table that would relate the two to where you had a schema like
1. numberstable
---------------------------
| number_id | number_name |
---------------------------
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
.
.
.
---------------------------
2. testtable
---------------------
| test_id | sthelse |
---------------------
| 1 | text1 |
| 2 | text2 |
| 3 | text3 |
--------------------
3. numbersintesttable
-----------------------
| test_id | number_id |
-----------------------
| 1 | 2 |
| 1 | 4 |
| 1 | 5 |
| 1 | 6 |
| 2 | 4 |
| 2 | 8 |
| 2 | 7 |
| 2 | 1 |
-----------------------
So the new table would be a many-to-many join table that you could use to get all your needed data in a single query by utilizing the type of joins you want (INNER, OUTER, etc.)