How to bind string column data - php

I want to write a php script to compare rows from the database and then adds the values together if payment_id are matches. Based on payment_id:
Example:
+----+------------+-----------+--------+
| id | payment_id | cheque_id | amount |
+----+------------+-----------+--------+
| 1 | 1000 | MB101 | 20 |
| 2 | 1000 | MB102 | 20 |
| 3 | 1111 | MB113 | 20 |
+----+------------+-----------+--------+
Result required
+------+--------------+----+
| 1000 | MB101/MB102 | 40 |
| 1111 | MB113 | 20 |
+------+--------------+----+
Trying to merge cheque column as string. For 'Amount' column I know that should be use SUM.
Any suggestions is appreciated,
Thanks

use GROUP BY
select payment_id,group_concat(cheque_id SEPARATOR '/') as cheque_ids,
SUM(amount) as amount
FROM table name
GROUP BY payment_id

I found the solution
SELECT payment_id, group_concat(cheque_id SEPARATOR '/') AS cheque_ids,
SUM(amount) AS amount
FROM tbl_name
GROUP BY payment_id

Related

Query mysql when reach row then stop? show the queue number

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)

Select foreign key (group) where is the biggest match

I have three tables group_sentences, group_sentences_attributes and group_senteces_categories.
I have an attributes array which I am using in query with IN (after implode).
Then I have one category ID because they are stored recursively, so no need for an array.
I need to select one group number where is the biggest match for $attributesArray and of course category too.
Here is table group_sentences_attributes
+-----+-------+-----------+
| id | group | attribute |
+-----+-------+-----------+
| 1 | 1 | 3564 |
| 2 | 1 | 3687 |
| 3 | 1 | 3689 |
| 4 | 2 | 3687 |
| 5 | 2 | 3564 |
+-----+-------+-----------+
Here is group_sentences_category
+-----+-------+----------+
| id | group | category |
+-----+-------+----------+
| 1 | 1 | 1564 |
| 2 | 1 | 1221 |
| 3 | 1 | 1756 |
| 4 | 2 | 1358 |
| 5 | 2 | 1125 |
+-----+-------+----------+
Here is my query, but I am afraid that it won't do the job done.
SELECT group_categories.group
FROM group_categories, group_attributes
WHERE group_categories.category = '$category'
AND group_attributes.attribute IN ($attributesArray)
GROUP BY group_categories.group
ORDER BY count(group_attributes.attribute)
Any help would be appreciated, thanks.
First, the table in your query do not match the tables in the question. I am guessing they are simply missing the "sentence". Then, you have no join clause. Simple rule: Never use commas in the from clause.
group is a lousy name for a column, because it is a keyword in SQL. The following may be what you are looking for:
SELECT gc.groupid
FROM group_sentences_attributes sa JOIN
group_sentences_category sc
ON sa.groupid = sc.groupid
WHERE sc.category = '$category' AND
sa.attribute IN ($attributesArray)
GROUP BY sa.groupid
ORDER BY count(sa.attribute);
If you only want one row, then add LIMIT 1 to the end.

how to merge multiple rows with same id mysql

I'm new posting here but the community have been my best resource on my projects so far.
I'm a dumb/dummy Mysql "wanna be" and I'm in the middle of a project that is making me go mad.
I have a table from wordpress plugin buddypress that pairs meta_key and meta_values in order to create something akin to a taxonomy. My duty is to use these paired values to implement an advanced group search. Here is the original table:
--------------------------------------------
id | group_id | meta_key | meta_value
--------------------------------------------
1 | 1 | time-zone | Kwajalein
2 | 1 | playstyle | hardcore
3 | 1 | recruiting-status | Open
4 | 1 | ilvl | 115
5 | 1 | main-raid | Final Coil of Bahamut
6 | 1 | voicechat | fc.teamspeak3.com
etc....
Using a view I managed to create a more friendly searchable table for begginers :
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 | | |
1 |Kwajalein | |
1 | | hardcore |
1 | | |
1 | | | Final Coil of Bahamut
1 | | |
And here is the view code:
SELECT distinct
group_id AS 'gid',
IF(meta_key='recruiting-status',meta_value,'') AS 'Recruitment',
IF(meta_key='server',meta_value,'') AS 'server',
IF(meta_key='time-zone',meta_value,'') AS 'tzone',
IF(meta_key='main-raid',meta_value,'') AS 'raid',
IF(meta_key='raid-days',meta_value,'') AS 'days',
IF(meta_key='playstyle',meta_value,'') AS 'playstyle',
IF(meta_key='raid-progression',meta_value,'') AS 'progression',
IF(meta_key='raid-time',meta_value,'') AS 'time',
IF(meta_key='tanker-spot',meta_value,'') AS 'tank',
IF(meta_key='healer-spot',meta_value,'') AS 'healer',
IF(meta_key='melee-dps-spot',meta_value,'') AS 'melee',
IF(meta_key='ranged-dps-spot',meta_value,'') AS 'ranged',
IF(meta_key='magic-dps-spot',meta_value,'') AS 'magic',
IF(meta_key='ilvl',meta_value,'') AS 'ilvl',
IF(meta_key='voicechat',meta_value,'') AS 'voice',
IF(meta_key='voicechatpass',meta_value,'') AS 'voicep',
FROM wpstatic_bp_groups_groupmeta
The point is, I need to merge that result (view) so all the group_id=1 or 2 or 3, etc stand in one single row, like this:
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 |Kwajalein | hardcore | Final Coil of Bahamut
2 |SaoPaulo | regular | Second Coil of Bahamut
etc
Can anyone help me there?
Just surround your IFs in a MAX, or another aggregate function that will capture the non-empty strings (e.g., GROUP_CONCAT), and add a GROUP BY group_id add the end. For example,
SELECT
group_id AS gid,
MAX(IF(meta_key='recruiting-status',meta_value,'')) AS 'Recruitment',
MAX(IF(meta_key='server',meta_value,'')) AS 'server',
...
FROM wpstatic_bp_groups_groupmeta
GROUP BY group_id

How to get the value in field by new line and update into the column in table with same id in php mysql?

i want to ask how to get the value of the column and update into another column with same id.. this data came from email piping and i want to separate the value of the body and update into the expense , net and gross field..
i hope you can help me guys.. thank you
+------+---------+--------------------+---------+----------+-------+----------+
| id | subject | body | expense | net | gross | email |
+------+---------+--------------------+---------+----------+-------+----------+
| 1 | Sales | Expense = 2000 | 0 | 0 | 0 | ex#ex.com|
| | | netIncome = 5000 | | | | |
| | | Gross = 10000 | | | | |
+------+---------+--------------------+---------+----------+-------+----------+
Assuming that you have one \n for the line break and having the same pattern for the body, lets say we have the following
mysql> select * from test \G
*************************** 1. row ***************************
id: 1
body: Expense = 2000
netIncome = 5000
Gross = 10000
Now to get each amount we can use substring_index something as
mysql> select
substring_index(substring_index(body,'=',-3),'\n',1) as expense,
substring_index(substring_index(body,'=',-2),'\n',1) as netincome,
substring_index(body,'=',-1) as gross from test;
+---------+-----------+--------+
| expense | netincome | gross |
+---------+-----------+--------+
| 2000 | 5000 | 10000 |
+---------+-----------+--------+
So for the update it could be as
update your_table
set
expense = substring_index(substring_index(body,'=',-3),'\n',1),
net = substring_index(substring_index(body,'=',-2),'\n',1),
gross = substring_index(body,'=',-1) ;

Dot product of one row against all rows in a many-column mySQL table

Background:
I have a ~400,000 row table which looks like the following:
+---------+--------+------+-------+------+-----+--------+
| ID | WORD | COL0 | COL1 | COL2 | ... | COL500 |
+---------|--------+------+-------+------+-----+--------+
| 0 | DOG | -0.73| 0.77 | 0.15 | | -0.55 |
| 1 | CAT | 0.41 | -0.57 | 0.61 | | 0.00 |
| 2 | HOUSE | 0.40 | 0.32 | -0.23| | 0.52 |
| ... | | | | | | |
| 400000 | LOVE | 0.51 | 0.59 | 0.01 | | -0.10 |
+---------+--------+------+-------+------+-----+--------+
Each col# represents a dimension of a 500 dim vector.
Problem:
Given a particular WORD value (they are unique), I want to find the 100 WORDs which are most similar to it based on the dot product (so an identical WORD vector will have a dot product of 1). So for the WORD 'CAR' I might get:
+--------+------+
| WORD | DOT |
+--------+------+
| CAR | 1 |
| TRUCK | 0.89 |
| SEDAN | 0.86 |
| VEHICLE| 0.81 |
| ... | ... |
| BIKE | 0.62 |
+--------+------+
So (to reiterate) I need to get the dot product of 'CAR' with every other word and sort it descending, and limit it to 100 results.
Potential solutions:
This SO question is very similar and was helpful, but I don't properly understand how to apply it ('garden' is being referred to as a table??).
Dot product in an SQL table with many columns
In the linked SO answer, 'garden' is a table: it's the table t, but aliased to garden, but limited to a single row (the one for the row with word 'GARDEN').
And for your particular question, you'd need to append 'ORDER BY DOT DESC LIMIT 100' to the end of the query.
Perhaps renaming it makes it clearer?
select allwords.*,
(allwords.col0 * word_of_interest.col0 +
allwords.col1 * word_of_interest.col1 + . . .
allwords.col500 * word_of_interest.col500
) as DOT
from allwords
cross join
(select allwords.*
from allwords
where `WORD` = '$THE_WORD_I_WANT_EG_CAR'
) as `word_of_interest`
order by `DOT` DESC LIMIT 100;
As the other answer says, I'd expect this to be fairly slow. If your COLn vector values are fairly static I'd consider pre-computing them and storing those results in a separate table that you'd query.

Categories