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.
Related
I am building a database for a website that will have multiple users type.
Admin
Student
Teacher
I want The user ID for each type to start with a specific number then auto-increment when new user register
For example
The First admin ID should be 01
The 10th student ID 110
The 100th teacher 2100
How can I do that in MySQL?
You can use window functions:
select
u.*,
row_number() over(partition by user_type order by id) + case user_type
when 'Admin' then 0
when 'Student' then 99
when 'Teacher' then 1999
end as new_id
from users
This assumes that column id is the primary key of the table. I would not recommend storing this information, which can be dynamically computed as shown above. If you are going to use this on a regular basis, you can create a view.
You should probably store students, admins, and teachers in different tables. Presumably, they are different entities in your data model -- for instance, "students" attend classes but "teachers" teach them.
Then if you really want you can set the auto_increment value to a different starting value in each table.
If you are just registering users for online access and your data model has nothing to do with the operations of a school, then just use an auto-incremented id. I strongly do not recommend encoding additional information in the id. It is available in the type column of the table.
If you really want ids that represent the different groups, you can add a computed column. I would recommend using strings for this:
alter table users add typed_user_id as
(concat(left(user_type, 1), lpad(user_id, 4, '0'))
I have a table called sitekeys:
In this site_key contains a unique license. And The field called licenses contains the no of times it can be used. the org_id field contains the company to which it has been assigned.
Now the activation table:
in this table site_key contains the keys assigned to the device.
Now I want to count The keys used for a company.
Like for XYZ company i has give 10 licenes then i want to know how many of them are used by it, so for that i can count(site_key) in activation table.
So what should be my query here?
I think i wil have to use Group_BY to do so.
I want show the keys i gave to a company and the keys they have used
i don't know exactly the design of your table so safely i have to join the two table
SELECT a.org_id, a.licenses, b.used_key from sitekeys a
LEFT JOIN
(
SELECT site_key, user_id, count(site_key) as used_key
FROM
activation b
GROUP BY site_key,user_id
) b
on a.site_key = b.site_key
$this->db->select("count(activation.site_key)");
$this->db->from("activation");
$this->db->join("sitekeys");
$this->db->group_by("sitekeys.site_key");
$this->db->get();
Try this
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.
Alright, so I understand how to do a mysql query to return results based on one search parameter. But I have a table that contains usernames and data. The data should be unique to each username, but I want to return any instances where that data might overlap over multiple usernames. An example would be
Username | Data Field
----------------------------------------------
Test | Abcd1234
Test2 | efgh5678
Test3 | Abcd1234
Test4 | efgh5678
I want my php script to return all instances where duplicate entries are found in the data field. Note that, neither the data field nor username field are unique in this table. The table gets populated whenever the user completes an action, so there should be many entries for each username, but each time they should have the same data field. I only want to check when two different usernames have the same data field. Does anyone have any idea how this can be done in php or a mysql select statement? It may take more than one query and that is okay. I've tried searching on how to find duplicate emails based on usernames, but the results I found were more on preventing duplicate registrations in the first place.
Basically, you want to count the number of unique users for each data field. This is an aggregation query:
select data, count(distinct username) as numusers
from table t
group by data;
You should be able to get the unique user names with this query:
select data, count(distinct username) as numusers,
group_concat(distinct username) as users
from table t
group by data
having count(distinct username) > 1;
This will create a comma separated list of users "using" the same data.
Apologies if this is really stupid but I don't have any experience in php and mysql to know how things should be done. I have a customer table in a mysql db and a group table:
customers - ID name email phone group
groups - ID name description
So I need to assign groups to customers if necessary, this can be more than one group to each customer. So e.g. customer 1 is in group 4,5,6
What way should I assign groups in the group column of the customer table. Should I just add the group ID's separated by commas, then just use explode when I need to get the individual ID's out?
Maybe this isn't the right approach at all, could someone enlighten me please. I would appreciate knowing the right way to do this, thanks.
Do not store multiple IDs in one column. This is a denormalization that will make it much harder to query and change your data, as well as hurting performance.
Instead, create a separate CustomerGroup table (with CustomerID and GroupID columns), and have one row per Customer/Group relationship.
Here is an example of tables to show how you should implement this :
Table 1 CONSUMERS:
id name email
1 john john#something.com
2 ray ray#something.com
Table 2 GROUPS :
id group_name description
1 music good music group
2 programming programmers
Table 3 CONSUMERS_GROUPS
consumer_id group_id
1 1
1 2
2 1
Now the table 3 is listing consumers ids which belong to which group id.
This type of relationship is called one to many relation where, one consumer can have many groups. Reverse might also be true where one group can have many consumers. In that case relationship is called many to many
Should I just add the group ID's separated by commas, then just use explode when I need to get the individual ID's out?
No! If you do that then you won't quickly be able to (for example) query for which users there are in a specific group.
Instead use a join table with two columns, each of which has a foriegn key constraint to the corresponding table.
group_id customer_id
4 1
5 1
6 1