Querying entries by IDs aggregated by two different tables - mysql

I have the following table structure
table object_to_profile
objectID | profileID
1 | 1
2 | 1
3 | 1
2 | 2
table object_to_task
taskID | objectID | profileID
1 | 1 | 1
1 | 4 | 1
1 | 2 | 2
The table object_to_task is built the following way:
I show the user checkboxes, which basically represent the object_to_profile table.
The user can select objects out of the table and save them into the object_to_task table.
The Administrator can later remove the object from the table. But if the user has already selected some of the object from the profiles, it should be still visible to him. So I need a query to select all object that are currently in the profiles plus all objects that were in the table and have been selected by the user.
As you can see, objectID 4 is no longer in the object_to_profile table, but has been selected by the user.
What would be a way to the objectIDs together?

As far as i understand, you just need to union 2 result sets
select ObjectID from object_to_profile
union
select ObjectID from object_to_task where taskID = 1

Related

How to access the keys in array of items in Sortable-Input Yii2

I'm having a hard time trying to figure how to resolve this.
I would like to know how can I access and use the keys in the items array, for use in a batchInsert?
Given the next scenario: I have a tableA like this
id | tableB_id | tableC_id
Where id_tableA is auto incremental, and both tableB_id and tableC_id are FK.
I need to insert in tableA multiple id from tableB which where selected from a connected Sortable, and the same id from tableC, wich was selected from a dropdown, resulting in something like this:
id | tableB_id | tableC_id
--------------------------
1 | 2 | 4
2 | 3 | 4
3 | 5 | 4
. | . | 4
n | x | 4
There’s no problem when I select only 1 item and submit the form, but when I select two or more I can’t submit because obviously the tableB_id can accept only integer.
How about changing your Dosis ID to a string, and explode(',', $dosisId) it to get an array of the ids?

MySQL - Is it possible to sort data from one table based on the presence of data in another?

I have a table that I want to be able to sort based on the existence of data in another, related table, but I'm not sure what I want to do is possible in a single query.
For example, say I have a Products table and a Notifications table. Each table has a bunch of columns, but the important ones for this purpose is an Active column, and a foreign key in the Notifications table that references the Products table. Each row in the Products table may be referenced 0 to N times in the Notifications table.
Products Notifications
ProductID | Active NotificationID | ProductID | Active | Type
----------+------- ---------------+-----------+--------+-----
1 | 1 1 | 2 | 1 | 2
2 | 1 2 | 3 | 0 | 1
3 | 1 3 | 3 | 1 | 1
4 | 1 4 | 5 | 1 | 1
5 | 1 5 | 3 | 1 | 1
One use case I'd like to support is to sort the data from the Products table based on whether or not there is an active Notification of a particular Type (Type=1) for the Product. So in the above example, Products 3 and 5 to be collated first or last, but all five products should still be in the result set.
I haven't been able to figure out a way to manage this in a single SELECT statement. I can easily pull just the Products that do or don't have an active Notification of a certain type, but I can't figure out a way to get them all at once and sort them based on that. Is it possible or do I just need to run a couple of separate queries?
What you want is accomplished through a join and aggregation. I would suggest summarizing the notifications table as a subquery to get what you want:
select p.*
from products p left join
(select productId, count(*) as cnt
from notifications n
where active = 1
group by productid
) n
on p.productid = n.productid
order by (n.productid is not null) desc;
This structure gives you the flexibility of using the existence (as shown above), or the count, or including the count in the select list.

MYSQL query fetching DATA from two table using IN method one as composition of multiple data

I have two tables
one as td_job which has these structure
|---------|-----------|---------------|----------------|
| job_id | job_title | job_skill | job_desc |
|------------------------------------------------------|
| 1 | Job 1 | 1,2 | |
|------------------------------------------------------|
| 2 | Job 2 | 1,3 | |
|------------------------------------------------------|
The other Table is td_skill which is this one
|---------|-----------|--------------|
|skill_id |skill_title| skill_slug |
|---------------------|--------------|
| 1 | PHP | 1-PHP |
|---------------------|--------------|
| 2 | JQuery | 2-JQuery |
|---------------------|--------------|
now the job_skill in td_job is actualy the list of skill_id from td_skill
that means the job_id 1 has two skills associated with it, skill_id 1 and skill_id 2
Now I am writing a query which is this one
SELECT * FROM td_job,td_skill
WHERE td_skill.skill_id IN (SELECT td_job.job_skill FROM td_job)
AND td_skill.skill_slug LIKE '%$job_param%'
Now when the $job_param is PHP it returns one row, but if $job_param is JQuery it returns empty row.
I want to know where is the error.
The error is that you are storing a list of id's in a column rather than in an association/junction table. You should have another table, JobSkills with one row per job/skill combination.
The second and third problems are that you don't seem to understand how joins work nor how in with a subquery works. In any case, the query that you seem to want is more like:
SELECT *
FROM td_job j join
td_skill s
on find_in_set(s.skill_id, j.job_skill) > 0 and
s.skill_slug LIKE '%$job_param%';
Very bad database design. You should fix that if you can.

Selecting multiple rows of data from a subtable along with the parent row table in MySQL

I have two tables in my database roughly laid out as below
Client
id | other data
1 | data 1
2 | data 2
3 | data 3
4 | data 4
Employee assigned
id | clientid | employee
1 | 1 | Fred
2 | 1 | Jim
3 | 3 | Peter
4 | 4 | Fred
5 | 4 | Peter
6 | 4 | James
Is there a way to return the client table with all of the employee records from the employee table? Ideally I would want both the id of the row and the employee name so I don't think I can use GROUP_CONCAT to collect more than one column. There is also no guarantee that there will always be an entry for each client in the employee table so it would need to return an empty/null result for those clients.
I have tried two ways to get this so far in php, both are pretty inefficient in one way or another.
(A) The first was to effectively make a new database connection half way through the first one to return the list of employees associated with that client id but obviously that results in a significant number of database connections being made for just one query.
(B) The second was to pull the entire employee table into an array at the beginning of the code and search through that array to pull out the rows relating to that client id which then have the employee name and row id in. Over time this is going to end up with a pretty large array being dumped into php instantly.
Is there some way of pulling this out using just a single SQL query? I'm not all that fussed how the data would come out as I am sure I could sort through it in the php at the other end, but perhaps something along the lines of
Results
id | other data | employees
1 | data 1 | 1,Fred;2,Jim
2 | data 2 |
3 | data 3 | 3,Peter
4 | data 4 | 4,Fred;5,Peter;6,James
If this has been asked before somewhere I apologise, but I have been searching around a bit over the last week and haven't found all that much to help.
SELECT e.*, c.other_data FROM Employee e
INNER JOIN Client c ON e.client_id = c.id
So, what you end up with is all the information from both tables grabbed in one query. So, the data would be:
id client_id Employee other_data
1 1 Fred data 1
2 1 Jim data 1
3 3 Peter data 3
4 4 Fred data 4
5 4 Peter data 4
6 4 James data 4

HTML listing of recordset, resulting from a join on two tables that relate one-many

I have two tables, that relate via a one-to-many relationship i.e
tableOne (1)----------(*) tableTwo
Given the basic schema below
tableOne {
groupID int PK,
groupTitle varchar
}
and
tableTwo {
bidID int PK,
groupID int FK
}
Consider the two tables yield the following record-set based on joining the tables on the tableOne.groupID = tableTwo.groupID,
tableOne.groupID | tableOne.groupTitle | tableTwo.bidID | tableTwo.groupID
________________________________________________________________________________
1 | Physics Group | 1 | 1
2 | Chemistry Group | 2 | 2
2 | Chemistry Group | 3 | 2
1 | Physics Group | 4 | 1
I would like to list such a record-set in an HTML table as follows:
tableOne.groupID | tableOne.groupTitle | tableTwo.bidID | tableTwo.groupID
________________________________________________________________________________
1 | Physics Group | 1 | 1
| Physics Group | 4 | 1
2 | Chemistry Group | 2 | 2
| Chemistry Group | 3 | 2
I'm interested in finding out if this can be done in SQL, or alternatively finding out ways of listing such a record-set in HTML using good standards.
The solution that comes to mind is simply iterating through the record-set and leveraging a sentinel to list all records with the same tableOne.groupID grouped in a single row <tr> - and also listing tableOne.groupIDs once as a unique identifier of that record-group. However I don't want to go down that path as I would like to avoid mixing code with HTML if possible.
You can order the sql results using the ORDER BY clause.
So if you add
ORDER BY tableOne.groupID ASC, tableTwo.bidID ASC
in your query, you are half-way there.
Next step is to loop and print the recordset from your asp page, but also check if the last groupID is different than the current, in order to decide whether to show it or not..