PHP&MySQL: Combine more lines and put it in array [duplicate] - php

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.)

Related

SQL Query - display join result in one result row [duplicate]

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

PHP and MYSQL select with 5 tables and multiple rows as a single array

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.

Add dynamic columns to MySQL result

I have a table in mysql, let's call it foo and it has a limitied amount of columns.
| id | name | date |
--------------------------
| 1 | bar | 2012-05-08 |
| 2 | buba | 2012-05-09 |
My users can add records to the table foo_field (stuff like, code, description, time...).
| id | name |
--------------------
| 1 | code |
| 2 | description |
| 3 | time |
In the table foo_field_value the values for the user-defined fields are stored, like so:
| id | foo_id | foo_field_id | value |
------------------------------------------
| 1 | 1 | 1 | b |
| 2 | 1 | 2 | Lalalala |
| 3 | 1 | 3 | 12:00 |
| 1 | 2 | 1 | c |
| 2 | 2 | 2 | We are go |
| 3 | 2 | 3 | 14:00 |
Ideally, I'd want one query which would give me a result like
| id | name | date | code | description | time |
------------------------------------------------------
| 1 | bar | 2012-05-08 | b | Lalalala | 12:00 |
| 2 | buba | 2012-05-09 | c | We are go | 14:00 |
Is this even possible without doing an inner join on the foo_fields_value table for every foo_field (generating the query with PHP by doing another query first).
It's possible to do it in just one, and quite simple.
We are going to modify the foo_field table a bit, adding a column corresponding to the foo table's id column, which I assume is the primary key.
so now we have
* foo
|------|
| id |
| name |
| date |
|------|
* foo_field
|-------------|
| foo_id |
| code |
| description |
| time |
|-------------|
Which means we can add the extra fields with one simple query:
SELECT * FROM foo
LEFT JOIN foo_field ON foo.id = foo_field.foo_id
Which will give us a result set of
| id | name | date | foo_id | code | description | time |
|----+-------+------------+--------+--------+-------------+----------|
| 1 | asdw | 2012-05-16 | 1 | asdasd | asdasd | 15:03:41 |
| 2 | fdgfe | 2012-05-18 | 2 | asdas | asdas | 15:03:41 |
| 3 | asdw | 2012-05-16 | 3 | asdas | asdas | 15:03:52 |
| 4 | fdgfe | 2012-05-18 | 4 | asdasd | asdasd | 15:03:52 |
I am still not sure I surely understood your question. If you want to create truly dynamic values and datastructures, I suggest you save a serialized array into a TEXT field in your database, but I also suggest you to overlook your solution if this is the case; if you want your solution to be able to grow, you want to manage as strict structures as possible.
What you are looking for is a pivot query. And since you have dynamic fields that need to converted to columns, check this article here about making automatic pivot queries
http://www.artfulsoftware.com/infotree/queries.php#523

select data from database Mysql query

tbl_pack_service;
+-------+---------+------------+
| ps_id | pack_id | service_id |
+-------+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 2 | 4 |
| 7 | 2 | 5 |
| 8 | 3 | 1 |
| 9 | 3 | 2 |
| 10 | 3 | 3 |
| 11 | 3 | 4 |
+-------+---------+------------+
ps_id is primary key
i am tying to make dynamic select list in php
i want a mysql query that can give service id which do not match with particular pack_id
like i have service_id 1,2,3,4,5
when i should select pack_id=1 then 3,4,5 should be displayed
and when i should select pack_id=2 then nothing should be displayed as it has all the 5 services.
thanks..
There are a few ways to handle this. The easiest is with a NOT IN subquery:
SELECT DISTINCT service_id
FROM
tbl_pack_service
WHERE service_id NOT IN (SELECT service_id FROM tbl_pack_service WHERE pack_id = 1)

Manipulating Foreign Keys output in MYSQL

I have a table connected to another table through foreign key;
First Table is the brand; it has a brand_id and brand_name fields
Second Table is the Product Line; it has a Line_id and line_name fields
Third Table is the Lines Offered; it has a id, Brand (which is a foreign key from the first table), and Line_name (which is a foreign key from the second table.
If i will look into the third table on mysql the fields contains the id numbers of the foreign keys.
My question is this, is it possible that the stored value will be the name itself and not the ID? or Is it possible to add a field on my third table named it as Brand_name which is a VARCHAR that will display the exact brand name from the first table. Example the values of my third table would be '1','3','The Brand Name of brand with id no. 3','25'; - If yes i dont have any idea how to do this.
Nothing is preventing you from doing this. You would simply add a brand_name field to the "third table"... The question begging to be asked is: why do you want to do this?
brands
_________________________
| brand_id | brand_name |
| 1 | brand1 |
| 2 | brand2 |
| 3 | brand3 |
| 4 | brand4 |
| 5 | brand5 |
-------------------------
lines
_________________________
| line_id | line_name |
| 1 | line1 |
| 2 | line2 |
| 3 | line3 |
| 4 | line4 |
| 5 | line5 |
-------------------------
linked
_________________________________________________
| id | line_id | brand_id | brand_name |
| 1 | 5 | 1 | brand1 |
| 2 | 5 | 2 | brand2 |
| 3 | 4 | 2 | brand2 |
| 4 | 4 | 3 | brand3 |
| 5 | 4 | 3 | brand3 |
| 6 | 3 | 4 | brand4 |
| 7 | 3 | 4 | brand4 |
| 8 | 2 | 4 | brand4 |
| 9 | 2 | 5 | brand5 |
| 10 | 1 | 5 | brand5 |
------------------------------------------------
That's your proposed setup. Now if we:
SELECT brand_id, brand_name FROM linked WHERE line_id = 4;
We would get:
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
But the same could be achieved without duplicate data (in large databases it's pretty unreasonable to have duplicate data like that), and without needing to update BOTH the linked and brand tables every time the brand name changes by using:
SELECT linked.brand_id, brands.brand_name
FROM brands, linked
WHERE linked.line_id = 4 AND brands.brand_id = linked.brand_id;
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
That answer didn't need to be that long. I was having fun making tables.

Categories