PHP MYSQL - Compare 2 comma separated lists - php

In my Database i have a field with a comma separated list of ids (itemids).
For an example i fill some rows with test data:
Row | itemids
1 => 2,5,8,11,22,45
2 => 4,6,9,13
3 => 1,3,5,16,23
4 => 6,12,18,21,24
Now i have a search string which contains all ids i want to serch for:
searchstring => 4,23,40
What i want to do now is to get all rows from the database which contains one or more values from my search string. From my example i should get the following rows:
Row | itemids
2 => 4,6,9,13
3 => 1,3,5,16,23
How do i do this? Can you give me an example SQL Query?
Thanks for help!

Query for searchstring => 4,23,40
select * from
item_table
where find_in_set(4,itemids)>0
UNION
select * from
item_table
where find_in_set(23,itemids)>0
UNION
select * from
item_table
where find_in_set(40,itemids)>0

Related

Querying a table with an array of json objects in RedbeanPHP

I'm having trouble working in Redbean PHP with querying a table with an array of json objects in a single field, and producing a report on it.
I want to get a report with subtotals of all staff with notes by the category. I know this should be easy/obvious but I'm just not getting it properly.
I have a database, with:
table clients
with columns:(int) client_id, (string) client_name, (array of json) notes
notes is an array of json with
(int) note_id, (int) note_category_id, (int) staff_id, (string) description, (memo) content, (date) note_date
table staff with columns (int) sid, (string) sname
table categories with columns (int) cat_id, (string) cat_name
So in pseudocode (since I'm still trying to figure this all out)
I need to run a query like: (with parameters in brackets)
R::getAll('Select * from Join (staff, categories, clients)
On (staff.sid=clients.services.staff_id, categories.cat_id=clients.services.note_category_id)
Where (clients.services.note_date Between [startdate] and [enddate],
categories.cat_name IN [chosencateg], staff.sname IN [pickednames])
Orderby sname Asc, cat_name Asc, note_date Desc ');
report output format:
Filters used: [picked filter choices if any]
-----------
[sname]
-- note category: [cat_name] 1
[note_date] 1 [description] 1 [content] 1
[note_date] 2 [description] 2 [content] 2
note category 1 subtotal
-- note category: [cat_name] 2
[note_date] 3 [description] 3 [content] 3
[note_date] 4 [description] 4 [content] 4
note category 2 subtotal
staff subtotal
[sname] 2 ...
I'm asking a fairly generic one because I'll have to work with a number of similar tables, and maybe seeing a query template will help my understanding.
Thanks for any help.
redbean is fantastic and - getAll is just scratching the surface and truly isn't working with redbean at all really... Read up on it here:
Here's a query template to get you started:
Query Template:
1)
R::getAll('Select * from Join (staff, categories, clients)
On (staff.sid=clients.services.staff_id, categories.cat_id=clients.services.note_category_id)
Where (clients.services.note_date Between :startdate and :enddate,
categories.cat_name IN (:chosencateg), staff.sname IN (:pickednames))
Orderby sname Asc, cat_name Asc, note_date Desc ');
You could also simply use:
2)
R::getAll('Select * from Join (staff, categories, clients)
On (staff.sid=clients.services.staff_id, categories.cat_id=clients.services.note_category_id)
Where (clients.services.note_date Between ? and ?,
categories.cat_name IN (?), staff.sname IN (?))
Orderby sname Asc, cat_name Asc, note_date Desc ');
The only difference is that query template 1 uses named parameters (so it's going to look to the array of params that you pass it to contain an associative array with parameters named in the same way as they are in the query). While template 2 requires simply an array of parameters with the indexes lined up with the order in which the ? marks appear in your query.
Anyway... the query should return an associative array of your columns representing rows. a var_dump would look something like this:
Array
(
[client_id] => 1,
[client_name] => "joe",
[noes] => "[
{note_id=1
,note_category_id=1
,staff_id=1
,description=blah blah
,content=blah blah blah blah
,content=some content for this note
,note_date=12/06/2018
}
]"
[sid] => 100,
[sname] => "some staff name"
[cat_id] => 100
[cat_name] => "some category name"
)
Notice how the notes field has just come out as a string (I know the json above is not properly formed json, I'm just trying to show an example).
I assume that what you want is to have that string converted into an array so you can work with it as if it were data and not a string. So the below should get you started with that:
Once you have it out of the database you should be able to access it like this:
$result = R::getAll($query,
['startdate'=> $mystartDate
,'enddate' => $myEndDate
,'chosencateg'=>$myChosenCategory
,'pickednames'=>$myPickedNames
]);
// this would output the json string to your screen
echo $result['notes'];
but it seems like you want to work with the json as if it were part of your data - so... you would need to decode it first.
// decode my notes field:
foreach(array_key($result) as $key) {
/* you are working with a multidimensional array in this loop
, use $key to access the row index. Each row index
will contain named column indexes that are column names from the database
*/
$result[$key]['decoded_notes'] = json_decode($result[$key]['notes'],true);
}
// I now have a new column in each row index, containing 'notes'
as another associative array
// the statement below now results in an array to string conversion error:
echo $result[someIndexNumber]['decoded_notes'];
So, I decided I would want this in MySQL (5.7) so as to use its capabilities. To do this I used string manipulation. MySQL 8 adds json_table functions which would have been nice to have.
I converted each array of JSON notes into lines of 'INSERT INTO temptable' to convert the array list into temptable rows,
one JSON object per row, adding the client_id to each object, then
EXECUTEing those statements.
SET #allnotes = (
SELECT json_arrayagg(REPLACE(`notes`,'{', CONCAT('{"id_client": ', id_client, ', '))) /* add id_client to each note object */
FROM clients
WHERE `notes` != '' AND `notes` != '[]' ); /* no empty note cases */
SET #allnotes = REPLACE(REPLACE(#allnotes ,'"[',''),']"','' ); /* flatten out outer array of each note */
SET #allnotes = REPLACE(REPLACE(#allnotes ,'{','("{'),'}','}")' ); /* INSERT INTO string formatting for the objects */
DROP TEMPORARY TABLE IF EXISTS jsonTemporary;
CREATE TEMPORARY TABLE IF NOT EXISTS jsonTemporary (anote json);
SET #allnotes = REPLACE(REPLACE(#allnotes,'[','INSERT INTO jsonTemporary (anote) VALUES '),']',';');
PREPARE astatement FROM #allnotes;
EXECUTE astatement;
/* totals */
SELECT concat(staff.last_name,", ",staff.first_name) AS sname,
categories.name AS cat_name,
count(anote->'$.id_client') AS cat_total,
FROM jsonTemporary
JOIN categories ON cast(anote->'$.note_category_id' as unsigned)=categories.id
JOIN clients ON clients.id_client=anote->'$.id_client'
JOIN staff ON staff.id=anote->'$.staff_id'
WHERE anote->'$.note_date' >= "2018-10-01" AND anote->'$.note_date' <= "2018-12-31"
GROUP BY sname, cat_name;
/* all notes */
SELECT concat(staff.last_name,", ",staff.first_name) AS sname,
categories.name AS cat_name,
anote->'$.note_date' AS n_date,
anote->'$.description' AS description,
anote->'$.content' AS content,
FROM jsonTemporary
JOIN categories ON cast(anote->'$.note_category_id' as unsigned)=categories.id
JOIN clients ON clients.id_client=anote->'$.id_client'
JOIN staff ON staff.id=anote->'$.staff_id'
WHERE anote->'$.note_date' >= "2018-10-01" AND anote->'$.note_date' <= "2018-12-31"
GROUP BY sname, cat_name;
DROP TEMPORARY TABLE IF EXISTS jsonTemporary;

Search Two Tables & Concatenate Answer

I am trying to search two tables, match the results and then concatenate the answer... Only finding results >= today's date. This will then give the user the option to delete the selected from the DB. So...
Table 1 called Prog_name
id prog_name
1 Breakfast
2 Mid Morning
3 Afternoon
Table 2 called talk_ups
id date_tx prog_name (prog_name value = prog_name.id)
1 2017-06-30 2
2 2017-07-03 1
3 2017-07-01 3
The result I am after is something like: "01-07-2017, Afternoon". But I do also need the talk_ups.id to ensure it only deletes the correct record.
I managed to figure out how to get the name to match the talk_ups.prog_name value:
'$sql. = "SELECT talk_ups.prog_name, prog_name.id as progID, prog_name.prog_name as theName FROM prog_name, talk_ups WHERE talk_ups.prog_name = prog_name.id";'
But I can't figure out how to do the two searches and end up with the right result and how to separate out the results to then concatenate them.
You can use JOIN with WHERE condition, e.g.:
SELECT pn.id, pn.prog_name, tu.date_tx
FROM prog_name pn JOIN talk_ups tu ON pn.id = tu.prog_name
WHERE tu.date_tx > NOW();

MYSQL summing values of multiple tables

I have multiple tables, I can list their rows to my website.
What I want is where last column has same value, I want to add up rows values and list as one line.
What I have:
Hioss
AUR
Top
1
1
0
Shen
Hioss
AUR
Top
1
1
0
Shen
Kanani
AUR
Jungle
1
1
0
Reksai
I don't want to get data like this.
If last column has same values (Shen) I want to sum int values and show as one line at my website.
What I want to do:
Hioss
AUR
Top
2
2
0
Shen
Kanani
AUR
Jungle
1
1
0
Reksai
My mysql query:
mysql_query("SELECT * FROM table1 UNION SELECT * FROM table2");
How can I do it? What should I do?
Try this. i dont know the fieldnames. You must change it
SELECT fieldnam1,fieldnam2, sum(fieldnam3),fieldnam4
FROM (
SELECT * FROM table1
UNION
SELECT * FROM table2
) as result
GROUP by fieldnam4;

GROUP BY clause ignoring rows and data

As we know GROUP BY clause return data by ignoring duplicate data by specifying particular GROUP BY 'user_id' , but i want to do that GROUP BY ignore table row but i want to combine data of that row in array , means i want all data but in filtered row
i want to something like
id | name | user_id
-------------------
1 abc 20
2 trt 19
3 sdf 20
4 khg 22
5 fdf 20
6 lnm 22
id | name | user_id
-------------------
1 abc,sdf,fdf 20
2 trt 19
3 khg,lnm 22
Use GROUP_CONCAT for this. Try this query -
SELECT id, GROUP_CONCAT(name) name, user_id FROM students GROUP BY user_id
The GROUP_CONCAT function concatenates strings from a group into one string with various options.
Update
To implement it in CakePhp -
$driverlocation_data = $this->DriverLocation->find(
'all',
array(
'conditions'=>array('DriverLocation.dispensary_id'=>$dispensary_id),
'fields' => array('id', 'GROUP_CONCAT(name) name', 'user_id')
'group'=>array('DriverLocation.driver_id')
)
);
Please try this query.
Select id, GROUP_CONCAT(name), user_id FROM table_name GROUP BY user_id
I have Used group_concat for name column.

MySQL closest values order by count

I have a table:
name count
Name 1 1
Name 2 1
Name 3 1
Name 4 1
Name 5 2
Name 6 2
If I select Name 2 (count = 1), how do I select the next and previous item (Name 3, Name 1)? After all values are the same.
How to solve my problem?
If you want a single query then I think this might be the query you are loooking for.
select * from table where coun=(select coun from table where name='name1')
and name!='name1'
I just rename count column to coun as count is an agg. function in mysql.
hope it works for you..

Categories