php, mysql join table columns with another table rows - php

I have issue getting results from two database table. here is what i have:
table A: 'courses'
math | history | geography | computer
1 | 2 | 3 | 4
and Table B
user_id | classroom_id | course
1 | 5 | 3
1 | 5 | 4
1 | 6 | 2
I returned the table A on a for each loop but I would like to check what courses the user 1 has to return true or false on any Table a columns.
Any help appreciated.
I need help not negative votes :(

You have your database set up wrong I believe. What you want is something like
Table_A:
PKEY | Course
1 | Math
2 | History
3 | Geography
4 | Computer
Table_B:
user_id | classroom_id | course
1 | 5 | 3
1 | 5 | 4
1 | 6 | 2
Then you could do something like
SELECT
TableA.PKEY,
TableA.Course,
TableB.user_id,
TableB.classroom_id,
TableB.course,
FROM TableA
LEFT JOIN TableB
ON TableA.PKEY = TableB.course
^^This will return the data from BOTH tables.
you see this line
ON TableA.PKEY = TableB.course
^^This is called the foreign key.
BIG GOTCHA: Make SURE that the columns for both of those ^^^ are set up EXACTLY the same. For instance, IF TableA.PKEY is an UNSIGNED INT(10), then TableB.course MUST also be UNSIGNED INT(10). They have to be identical for the join to work correctly.

Related

check if comma separated values exist in table

I have a table: users_group
id | group_id | user_ids
---|----------|---------
1 | 1 | 3
2 | 1 | 2
3 | 3 | 2
4 | 2 | 3
5 | 2 | 4
6 | 2 | 2
condition is that user_ids can be inserted only once. But in above case it is inserted for more than one group_id.
I am using this query to insert users_id field within foreach loop:
INSERT INTO users_group (group_id, user_ids) VALUES(2,3)
how can I prevent to insert duplicate user_ids
Is there any better query?
Yes, there is a better way to solve this problem, but the solution doesn't imply the query.
Instead of user_id column you should create a new column called user_id and add data like this:
id | group_id | user_id
1 | 1 | 3
2 | 1 | 4
3 | 2 | 3
4 | 2 | 4
5 | 2 | 2
6 | 3 | 2
7 | 3 | 3
8 | 3 | 4
This is called Many to Many relation and makes everything easier. After that you need to only JOIN the tables;
I think you want to start by creating unique index on the column that you want to have only unique values . In this case that it would be the user_ids column.

Mysql query, use field as column

I have the following table structure
+-------+------------+-----------+---------------+
| id |assigned_to | status | group_id |
+-------+------------+-----------+---------------+
| 1 | 1001 | 1 | 19 |
+-------+------------+-----------+---------------+
| 2 | 1001 | 2 | 19 |
+-------+------------+-----------+---------------+
| 3 | 1001 | 1 | 18 |
+-------+------------+-----------+---------------+
| 4 | 1002 | 2 | 19 |
+-------+------------+-----------+---------------+
| 5 | 1002 | 2 | 19 |
+-------+------------+-----------+---------------+
I would like to get the information in the following format
+-------+------------+-----------+
| | 1001 | 1002 |
+-------+------------+-----------+
| 1 | 1 | 0 |
+-------+------------+-----------+
| 2 | 1 | 2 |
+-------+------------+-----------+
So basically I am looking to use the assigned to field as the column names. Then the rows represent the status. So for example in the table we have two rows where user 1002 has a status of 2, therefore the sum is shown on that particular status row.
Please note that the group_id must be 19. Hence why I left out the row with id 3 on my table.
Can someone point me in the right direction. Im sure there is a name for this type of query, but I can't for the life of me put this into words. I have tried various other queries, but none of them even come close to this.
Marc B is right, there is no way to pivot a table -i.e. converting the content of a field into columns- unless you make some assumptions, like supossing that the values of assigned_to are somewhat fixed.
On the other hand, this is the kind of problems that can be solved by a program. It is not an easy program, but it can do the job.
I recently made a program similar to this in java, if you are interested I can post the core of it here.
you might want to read this article http://www.artfulsoftware.com/infotree/qrytip.php?id=523
i'd be something like
SELECT
assigned_to,
COUNT( CASE assigned_to WHEN '1001' THEN 1 ELSE 0 END ) AS '1001',
COUNT( CASE assigned_to WHEN '1002' THEN 1 ELSE 0 END ) AS '1002'
FROM table
WHERE group_by = 19
GROUP BY assigned_to WITH ROLLUP;
or something like that (i haven't tested this code.. )
in the article, he does it using SUM() you'd have to do it with COUNT() and add a WHERE constraint for the group_id
Hope this helps

Run While Loop Through All Distinct Values of A Column

This is an example MYSQL result
+----+---+
| A | B |
+----+---+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
+----+---+
I would like to run through every distinct in Column A and do something utilizing the values in Column B.
Let's say A has userids and B has foods. I would like to grab all the foods that user 1 likes and then shoot an email to 1, then grab all the foods that user 2 likes and email to her, and so forth. Would appreciate any suggestions.
If you want comma separated values, you can use GROUP_CONCAT
SELECT A, GROUP_CONCAT(DISTINCT B) foodList
FROM tableName
GROUP BY A
SQLFiddle Demo
Other Link
GROUP BY clause

MySQL, php table question (delete on an update)

I have the two following tables
table A
| id | name |
| 1 | bob |
| 2 | jill |
| 3 | jojo |
Table A is displayed by using checkboxes.
On the first go, the user checks all three checkboxes so you get the result in table B.
table B
| table_a_id | table_c_id |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
But the next time the user goes to edit, they UNCHECK '2' so that it's only:
1
3
How do I write my query (using either mySQL or php) so that TABLE B is updated to:
| table_a_id | table_c_id |
| 1 | 2 |
| 3 | 2 |
DELETE A,B
FROM A
LEFT JOIN B
ON B.table_a_id = A.id
WHERE A.id NOT IN (1,3)
Or use InnoDB with a Foreign key ON DELETE CASCADE, much simpler :)
DELETE
FROM TableB
WHERE (NOT table_a_id IN (1, 3))
AND (table_c_id = 2)

SQL string replace with ID statement

Is it possible to create a sql statement to do the following:
Table 1 lists all of the drug names, Table 2 contains all of the side effects.
Table 3 contains all the drug names by ID and all of the side effects separated by |.
Is there some kind of SQL query I can run to re-create Table 3 where the side effects separated by | are the side effect ID's from Table 2?
Table1
---------------------
id | drug_name
---------------------
1 | aspirin
2 | zoloft
3 | codine
Table2
---------------------
id | side_effects
---------------------
1 | rash
2 | hearing loss
3 | the plague
Table3
---------------------
id | drugs2sidefx
---------------------
1 | rash | hearing loss
2 |
3 | the plague | hearing loss
You don't need table3 this way.
it should be
drugs2se
---------------------
d_id | se_id
---------------------
1 | 1
1 | 2
3 | 2
3 | 3
Then you can get desired results with a query like this
SELECT d.name, group_concat(se.name) as effects
FROM drugs d, drugs2se dse, side_effects se
WHERE d.id=d_id AND se_id=se.id
GROUP BY (d.id)

Categories