I have a MySQL table that looks like this:
id | id_item |
1 | T0001 |
2 | T0002 |
2 | T0003 |
3 | T0004 |
and I want to change it like this:
id | id_item |
1 | T0001,T0002 |
2 | T0001,T0003 |
2 | T0001,T0004 |
3 | T0002,T0003 |
4 | T0002,T0004 |
5 | T0003,T0004 |
and I want save in table item_2 like this:
id | id_item |
1 | T0001 |
2 | T0002 |
3 | T0001 |
4 | T0003 |
5 | T0001 |
6 | T0004 |
7 | T0002 |
8 | T0003 |
9 | T0002 |
10 | T0004 |
11 | T0003 |
12 | T0004 |
Does anyone know how to do this by using PHP or MySQL?
You can generate this output via the following query:
SELECT
CONCAT(t1.id_item, ',', t2.id_item) AS id_item
FROM yourTable t1
INNER JOIN yourTable t2
WHERE
t2.id > t1.id
ORDER BY
t1.id,
t2.id
If you wanted to insert this data, you can use the above select to do an INSERT INTO ... SELECT into a new table. I mention new table, because it probably would not make much sense to store CSV and non CSV data in the same column. Actually, I don't at all recommend storing CSV data in the first place. Instead, just generate it if you need it in your presentation layer using a query similar to what I have given above.
Demo here:
Rextester
Related
I have the following MySQL table which is structured like that:
| id | bonus0 |
Now I want to add the following data set:
| id | bonus0 | bonus1 | bonus2 | bonus3 |
| 10 | 4582 | 2552 | 8945 | 7564 |
As you can see the columns bonus1 - bonus3 arenĀ“t created yet.
How would a php script/ query look like which checks if enough columns are already available and if not which will create the missing ones with consecutive numbers at the end of the word "bonus"?
So in the example the columns bonus1 - bonus3 would be created automatically by the script.
In reality (I mean a normalized relational database) you should have 3 tables. Lets call them people, bonuses and bonus_to_person
people looks like:
+-----------------+------------+
| person_id | name |
+_________________+____________+
| 1 | john |
+-----------------+------------+
| 2 | frank |
+-----------------+------------+
bonuses Looks like
+----------------+--------------+
| bonus_id | amount |
+________________+______________+
| 1 | 1000 |
+----------------+--------------+
| 2 | 1150 |
+----------------+--------------+
| 3 | 1200 |
+----------------+--------------+
| 4 | 900 |
+----------------+--------------+
| 5 | 150 |
+----------------+--------------+
| 6 | 200 |
+----------------+--------------+
bonus_to_person Looks like
+----------------+-----------------+
| bonus_id | person_id |
+________________+_________________+
| 1 | 1 |
+----------------+-----------------+
| 2 | 2 |
+----------------+-----------------+
| 3 | 2 |
+----------------+-----------------+
| 4 | 1 |
+----------------+-----------------+
| 5 | 1 |
+----------------+-----------------+
| 6 | 1 |
+----------------+-----------------+
This way, any ONE person can have unlimited bonuses simply by INSERTING into bonuses with the amount, and INSERTING into bonus_to_person with the bonus_id and person_id
The retrieval of this data would look like
SELECT a.name, c.amount from people a
LEFT JOIN bonus_to_people b
ON a.person_id = b.person_id
LEFT JOIN bonuses c
ON c.bonus_id = b.bonus_id
WHERE a.person.id = 1;
Your result from something like this would look like
+------------+----+-------+
| name | amount |
+____________+____________+
| john | 1000 |
+------------+------------+
| john | 900 |
+------------+------------+
| john | 150 |
+------------+------------+
| john | 200 |
+------------+------------+
You should be using this normalized approach for any database that will continue growing -- Growing "deeper" than "wider" is better in your case ..
// Get existing columns of the table
// $queryResult = run SQL query using PDO/mysqli/your favorite thing: SHOW COLUMNS FROM `table`
// Specify wanted columns
$search = ['bonus0', 'bonus1', 'bonus2', 'bonus3'];
// Get just the field names from the resultset
$fields = array_column($queryResult, 'Field');
// Find what's missing
$missing = array_diff($search, $fields);
// Add missing columns to the table
foreach ($missing as $field) {
// Run SQL query: ALTER TABLE `table` ADD COLUMN $field INT
}
I am doing a script want to calculate how many row record before an user record when t1.status is 1.
My table is t1, and the data as below:
+------+---------+------------+----------+----------+
| ID | name | desc | status | time |
+------+---------+------------+----------+----------+
| 1 | ABB | | 1 | 0325 |
| 2 | CCD | | 1 | 0236 |
| 3 | EEF | | 1 | 0325 |
| 4 | GGG | | 1 | 0000 |
| 5 | HIJ | | 2 | 1234 |
| 6 | KKK | | 1 | 5151 |
+---------------------------------------------------+
I was thinking about the query is something like (query row where status = 1 AND stop when reach $userid)
I would like to output to show user (Let's say username is GGG) as:
$userid = 'GGG';
then my output will be
<table><tr><td>Queue: GGG You came in 4 place, in front of you still got 3 person in queue, please be patient</td></tr></table>
How to I do the right query to get the number 4 and 3 ?
Thank you.
You can try something like this hope it helps :-
SELECT count(*) as COUNT FROM t1 WHERE id < (SELECT id FROM t1 WHERE userid = $userid)
I have the tables like this
tbl_post
+-----------+--------------+
| post_id | post_content |
+-----------+--------------+
| 1 | contentone |
+-----------+--------------+
tbl_category
+-------------+---------------+
| category_id | category_name |
+-------------+---------------+
| 1 | Politic |
| 2 | Social |
| 3 | Economy |
+-------------+---------------+
tbl_category_post
+------------------+-------------+---------+
| category_post_id | category_id | post_id |
+------------------+-------------+---------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
+------------------+-------------+---------+
then I want the output like this
+--------------+--------------------------+
| post_content | category |
+--------------+--------------------------+
| 1 | Politic, Social, Economy |
+--------------+--------------------------+
and then how to show the data like this using codeigniter, I really confused at all, anyone please help me!
Edit: With Codeigniter (not tested):
$this->db->select('post_id, GROUP_CONCAT(tc.category_name) AS category_name')
->from('tbl_category_post tcp')
join->('tbl_category tc', 'tc.category_id=tcp.category_id', 'left')
->group_by('tcp.post_id');
I suppose You need PHP loop method to loop this.
Use mysql GROUP_CONCAT function:
SELECT post_id, GROUP_CONCAT(tc.category_name) AS category_name
FROM tbl_category_post tcp
LEFT JOIN tbl_category tc ON tc.category_id=tcp.category_id
GROUP BY tcp.post_id
hello can somebody give me idea how to do this problem? let say for example i have two table "sales" and "rsales" . and i have this ff data for my tables.
data from sales
receipt | date | total | 1st_id | last_id |
129999 | 09/26/2013 | 1220 | 1 | 2 |
139999 | 09/27/2013 | 2320 | 3 | 4 |
data from rsales
id | product_name |
1 | 33uf |
2 | 44uf |
3 | 7sss |
4 | 8sss |
and this is my view from my web page when do reports
receipt | date | total | 1st_id | last_id | + |
129999 | 09/26/2013 | 1220 | 1 | 2 | + |
139999 | 09/27/2013 | 2320 | 3 | 4 | + |
so here it is. my problem is that when i click "+" it will select data from rsales and then display. so let say i cllick "+" where receipt is 129999 . so the expected output must be something like this.
receipt | date | total | 1st_id | last_id | + |
129999 | 09/26/2013 | 1220 | 1 | 2 | + |
id | product_name |
1 | 33uf |
2 | 44uf |
139999 | 09/27/2013 | 2320 | 3 | 4 | + |
so why it display like this? it is because 1st_id and last_id from sales = id 1 and 2 from rsales. it is hard to explain but i think the example i gave is enough to understand what i want to do. hoping your idea. i really need idea.
if you have it set up the way I think you have it set up you'll need two queries:
SELECT 1st_id, last_id FROM sales WHERE receipt=?
and then
SELECT Id, product_name FROM rsales WHERE Id>=[variable from 1st_id] AND Id<=[variable from last_id];
if I were doing it I would have set up 3 tables
a sales_rsales with 3 fields, id, sales_receipt, rsales_id.Get rid of 1st_id and last_id use that to track the products per receipt then the SQL will be easy:
SELECT rsales.Id, rsales.product
FROM rsales JOIN sales_rsales ON rsales.Id=sales_rsales.rsales_id
JOIN sales ON sales.receipt=sales_rsales.sales_receipt
WHERE sales.receipt=?
if i have this table:
perma_table:
+-------+-------+
| A | B |
+-------+-------+
| a | 5 |
| c | 7 |
| a | 8 |
| b | 9 |
| a | 7 |
| c | 6 |
| a | 8 |
+-------+-------+
i want to make
inserted_table:
+-------+-------+-------+
| A | B | C |
+-------+-------+-------+
| a | 5 | 1 |
| a | 7 | 2 |
| a | 8 | 3 |
| a | 8 | 4 |
| b | 9 | 5 |
| c | 6 | 6 |
| c | 7 | 7 |
+-------+-------+-------+
how to do this?
im using code igniter (php), is this all of this method can be done by using query, without using CI active record/php function?
You can do it with a single query
CREATE TABLE inserted_table as
SELECT A,B,#num:=#num+1 AS C
FROM perma_table,(select #num:=0) temp
ORDER BY A,B;
If you want just a query to do it, its so simple:
Insert it normally, after inserted you can select
SELECT * FROM table ORDER BY A, B, C ASC
But if you want to keep this order, without at any select without order you can create a view or a new table like
CREATE VIEW inserted_table AS
SELECT A, B, #id = #id+1 AS C FROM perma_table
ORDER BY A,B;
OR a new table (no view)
CREATE TABLE inserted_table AS
SELECT A, B, #id = #id+1 AS C FROM perma_table
ORDER BY A,B;
With view you can insert any value on perma_table, the inserted_table will automaticaly generated, if you choose the second case, you heave to recreate the table averytime when you edit perma_table