permanently merge and concatenate multiple columns in laravel - php

I am trying to merge 3 clumns into a single column,
here is the logic:
I have a serial number table with:
number
prefix
separation
I want to merge them into a single column named serial_number in the same table.
in another table of orders I will recover the serial_number in the controller, at store it will update the number + 1 in the table of serial numbers,
I tried to update the number and concatenate the 3 columns in the controller at each recording it works ,
but the problem is that the user can modify the order of the 3 columns in the settings.
is there a method to permanently merge and concatenate the 3 columns without doing it on the order controller so if the user changes the order automatically it will update the column serial_number ,

Related

How to retrieve count values as multiple columns using group by in single query in MySQL?

I am writing a complex MySQL query. My actual query is more complex than I mentioned below.
I have a table named example and columns are id, name, option_1, option_2 . Of course id column is PK . I want to retrieve like this:
SELECT `id`,`name`,count(`option_1`),count(`option_2`)
My problem is I want to use "GROUP BY `id`" for count(`option_1`) and "GROUP BY `name`" for count(`option_2`) respectively. Now I have to break down it into multiple code in my php code.
How can I achieve what I want in a single query?
What you're asking for doesn't make a ton of sense. You want option 1 grouped by id and option 2 grouped by name, and you want to show all four columns in one result set.
First of all, if id is a primary key, then the count will just be the number of rows in the table since there will be no duplicate ids.
Second, even if id wasn't a primary key, and there were duplicate values, the grouping is different, so the counts represented in your result set would be grouped incorrectly.
Given that id is a primary key, perhaps your intention is actually to get a total count of the rows in the table. Perhaps this query would suit your needs?
SELECT
name,
COUNT(option_2) AS options
FROM
example
GROUP BY
name
UNION ALL
SELECT
'Total' AS name,
COUNT(*) AS options
FROM
example
This should get you a count of all the option_2 values, grouped by name, with the final row having a name of 'Total' and the count being the total number of rows in the table.
Aside from that, I'm not sure you're going to find the answer you're looking for due to the problems you would encounter matching up groupings.

Store multiple ID's in the same column?

Is it possible to store an array of ID's within a table column which reference ID's from another table so that I can do a select query and determine whether a specific ID is within that column?
Example scenario below.
I have 3 tables like this:
Table: tab_groups with Columns: id, name
Table: tab_users with Columns: id, name, groups
Table: tab_forums with Columns: id, title, req_groups
A user can belong to multiple groups, the ID's of the groups that they belong to are stored in the groups column.
A forum can have multiple required groups specified, which the ID's of are stored in req_groups. A user must belong to at least 1 group which matches with at least 1 required group in order to view the forum.
How should I store the groups and req_groups columns so that I can later do a SELECT query to check whether a user belongs to a group which matches with a group ID required to view the forum?
Also how would I update those columns to include new ID's or remove old ones?
Currently I just have a pretty inefficient method which involves storing them as a string with a separator (like 1,2,3,4), then splitting them in PHP and doing a comparison check in a loop.
For updating I just check whether a value exists, and add it onto the string.

PHP- Taking the Largest value from Mysql column of Unique ID

My current projects consist of Registration of families of different areas in a City. i want to Generate a Unique ID to each families in which i need this ID for another part of this project.Each area in the city have already a unique number eg: 855, 856, 857,etc
So my plan is to generate unique Family ID by combining unique number already have + text "FM"+ number (1,2,3....) and store this uid to DB along with family details.
For eg (Area-855): 855FM1, 855FM2, 855FM3....
if the last registered family uid is 855FM40 , then next family uid must be 855FM41. so i want to fetch the largest value from uid field. in above eg:, largest is 855FM41. how i do this. ?
i have simple logic that fetch all uid, then split it after "FM". then find largest ,etc
How i solve this problem.? is there any simple way other than this.?
Using ORDER command you can sort your data by ordering of one column ascending or descending.
so first we order FamilyID column descending (in sql we use DESC) and then we get the first row which has biggest FamilyID value using "LIMIT 0,1" command
Try this:
SELECT * FROM families ORDER BY FamilyID DESC LIMIT 0, 1
You should use two columns instead of one. For example this:
FamilyID
--------
855FM1
855FM2
Should be stored as:
CityID FMNumber
------ --------
855 1
855 2
This way the data should be easier to manage and less redundant. And yes, it is possible to define primary or unique keys over multiple columns.

mySQL primary key oddity

I have a script which INSERT's data into a table and then later on when you INSERT new data it DELETE's the previous record/s and INSERT's the current data set.
The only issue is that the primary key gets wacked.
e.g. first four rows
1
2
3
4
then when i delete these and enter new data
5
3
4
6
note: the above numbers represent primary key id auto incrementations
Why does the incrementation become confused almost?
Auto-increment number do not get confused. They are unique over the table and that is the only purpose they have.
If you select the data then the DB will grab the records as fast as possible and if you do not specify a specific order then the records are returned in an unpredictable order.
That means if you specify
select * from your_table
order by id
Then the records have incrementing numbers. If you delete records then the gabs won't be filled.
If you want to restart the numbers, use truncate table instead of delete. This will reset the counter to 0:
truncate table <your table here>;

Finding value in a comma-separated text field in MySQL?

I've got a database of games with a genre field that has unique ids in it that are separated by commas. It's a text field. For example: HALO 2 - cat_genre => '1,2' (Action,Sci-Fi)
I'm trying to make a function that calculates the total number of games in that genre. So it's matching 1 value to multiple values separated by commas in the db.
I was using SELECT * FROM gh_game WHERE cat_genre IN (1) which would find Action Games.
I'm trying to work this out, I thought I had nailed it once before but I just can't figure it out.
You need to create a many to many relation. like so
CREATE TABLE gameGenreTable ( id int NOT NULL PRIMARY KEY, genreID, gameID)
EDIT: if you're using InnoDB you can also create foreign keys on genreID and gameID..
I would add a UNIQUE key on genreID, gameID
then you can do a query like this
SELECT genreID,count(genreID) as count from gameGenreTable GROUP BY genreID;
-- and join in your other table to get the genre name (or just use the ID).

Categories