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)
Related
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.
Hi I have 2 table Offense table and User_jobs table
offense table:
crime_id |crime_type |casenumber|
---------+-----------+----------+
1 | 3 |1 |
2 | 3 |1 |
1 | 3 |2 |
12 | AA |2 |
user_jobs table:
casenumber |disposal_status |
-----------+----------------+
1 | yes |
1 | yes |
2 | no |
2 | no |
what i want is to count the number of rows with the same combination say crime_id=1 and crime_type= 3 but these must have a disposal status of yes in the user_jobs table.
i want to do this in mysql. pliz help
sorry but i am new to mysql. i now want to display the real names of those id not the id themselves.
the tables with these IDs are crime_category and Crime_type Crime_catgory
table:
category |crime_id |
-----------+----------------+
theft | 1 |
murder | 2 |
rape | 3 | 2 |
no |
Crime_type table:
Crime_type |id |
---------------+----------------+
administrative | yes |
criminal | yes |
You can do this with a simple inner join and an aggregate function:
select
o.crime_id,
o.crime_type,
count(*)
from
offence o
join user_jobs uj
on o.casenumber=uj.casenumber
where
uj.disposal_status='Yes'
group by
o.crime_id,
o.crime_type
This will pick up distinct combinations of the first two columns joined as they should tot he jobs table and only where the disposal_status is equal to 'Yes'
Edit: You would probably do really well to have a read of this Q&A that I put together for exactly this sort of situation - where I give you the code for it, but would like to explain this is a lot more detail. The Q&A explains why this type of thing (and many many others) work and how they do so:
How can an SQL query return data from multiple tables
Edit 2:
select
o.crime_id,
o.crime_type,
ct.category,
count(*)
from
offence o
join user_jobs uj
on o.casenumber=uj.casenumber
join crime_type ct
on o.crime_type=ct.crime_id
where
uj.disposal_status='Yes'
group by
o.crime_id,
o.crime_type,
ct.category,
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
I have been working with a postgresql database with a php project, and I am just trying to construct a query. This is the scenario.
Suppose I have several tables, FOO and BAR
Table FOO looks like this:
| foo_id | foo_name | foo_data |
----------------------------------
| 1 | john | son |
| 2 | jane | daughter |
| 3 | sam | son |
| 4 | sally | daughter |
Table BAR looks like this
| bar_id | bar_foo_id | bar_fooParent_id | bar_content |
----------------------------------
| 1 | 1 | 1 | yabba-dabba-doo |
| 2 | 2 | 1 | scooby-scooby-doo |
| 1 | 3 | 888 | don't have a cow, man |
| 2 | 4 | 999 | d'oh! |
I can't change the table schema, everything is as it is.
But - I can pass a value through PHP, let's call it PARENT, that represents a value from bar_fooParent_id (so I can pass 1,2,3...657,888,999 etc).
And - foo_id in table FOO maps to bar_foo_id in table BAR.
I would like to build a SELECT Query that combines data from both tables FOO and BAR. Something like:
SELECT BAR.bar_content, (and other BAR.columns) , FOO.foo_name, FOO.foo_data
FROM FOO,BAR WHERE bar_fooParent_id=".$PARENT." AND " ...?
where ? is a little confusing.
I need to grab rows for foo_name and foo_data from table FOO based upon the selected rows from table BAR. So if a value of 1 is passed to $PARENT, then bar_fooParent_id would be 1, I would get the first two rows from table BAR, and use their respective bar_foo_ids (with values 1 and 2) to grab the data from the rows of table FOO that have foo_ids of 1 and 2 (the first two rows in this case).
I have tried statements similar to those below (values are hard coded for simplicity)
SELECT * from BAR,FOO where BAR.bar_fooParent_id=1 AND (BAR.foo_id=1 OR BAR.foo_id=2) AND (FOO.foo_id=1 OR FOO.foo_id=3)
OR
select * from BAR where BAR.barr_fooParent_id=1 IN (SELECT foo_id,foo_name from kid WHERE foo_id=1 OR foo_id=3 )
without much success. Basically the data should return ideally as
foo_name | foo_data | bar_content | other BAR columns ... |
_________________________________________________________________
john | son | yabba-dabba-do | etc. |
jane | daughter | scooby-dooby-do| etc. |
(apologies for the formatting, not sure what is happening with this third table of results)
I'd appreciate it if someone can lend a hand in building this SELECT query for postgreSQL.
any ideas? and thanks.
Edward
Sounds like all you need is a simple join:
SELECT f.foo_name, f.foo_data, b.bar_content
FROM foo f
INNER JOIN bar b ON b.bar_foo_id = f.foo_id
WHERE b.bar_fooParent_id = 1
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)