I have 3 tables in mysql, table A, B and C.
C has a relationship with B (b_id) and B has a relationship with A (a_id).
that means A has many B, and B has many C. To select all, I have this query:
SELECT a.id, a.name, a.text, b.ptext, c.ctext
FROM tableA as a
JOIN
tableB as b
ON
(b.p_id = a.id)
JOIN
tableC as c
ON
(c.p_pid = b.pid)
WHERE a.id = 1
which returns this:
| ID | NAME | TEXT | BTEXT | CTEXT |
|----|-------|--------------|--------|--------|
| 1 | page1 | futkdvthsa_1 | post_1 | thing1 |
| 1 | page1 | futkdvthsa_1 | post_2 | thing2 |
| 1 | page1 | futkdvthsa_1 | post_2 | thing3 |
is there any chance of getting something like this:
id | name | text | posts
1 | page1 | futkdvthsa_1 | posts (post_1 = ( thing1 ), post_2 = ( thing2, thing3 ) )
Sigle queries for each table or would need to do in a php way?
P.S. this would be for a wordpress plugin with custom tables.
I guess the simplest way to do this is by doing it with php.
You would have to use a stored procedure to concatenate your fields with sql.
Related
I have been using this site for years now and this is the first time I'm asking a question here, so kinda scared right now :D
Here's what my problem is, I have got two tables. In table_a I got three columns and in table_b I got 5. So the setup right now looks something like this:
table_a
| r_id | foo | bar |
+------+-------+-----+
| 1 | dude | 5 |
+------+-------+-----+
| 2 | homie | 6 |
+------+-------+-----+
| 3 | bro | 7 |
+------+-------+-----+
table_b
| id | ada | rea | lm | cor |
+----+-------+-----+------+------+
| 5 | ching | ink | jk | 32.4 |
+----+-------+-----+------+------+
| 1 | momo | pal | lmao | 95.5 |
+----+-------+-----+------+------+
| 6 | mama | pen | lol | 26.9 |
+----+-------+-----+------+------+
| 4 | chac | pin | fun | 91.2 |
+----+-------+-----+------+------+
| 7 | chim | lap | funk | 82.4 |
+----+-------+-----+------+------+
| 9 | cho | kil | fin | 38.1 |
+----+-------+-----+------+------+
Now what I'm trying to do is to get all the data from table_a and then only get lm from table_b. I'm getting all the data from table_a like this:
SELECT r_id, foo, bar from table_a
I need to use the ids I get from bar column to get lm from table_b. So is there a way I can pass an array to only get the data based on the ids in an array? If not, then what would be the most efficient way to get those?
The output I'm expecting is jk, lol, funk.
Would appreciate any help, thanks!
You should be looking at using a JOIN to link the two tables together in 1 query...
SELECT r_id, foo, bar, lm
FROM table_a
JOIN table_b on bar = id
try inner join
SELECT a.r_id, a.foo, a.bar, b.lm from table_a as a inner join table_b as b on b.id=a.bar
Why not join?
select group_concat(lm) as lm_list
from table_b b
inner join table_a a on b.id = a.bar
You can use the GROUP_CONCAT() function, with this you would get jk, lol, funk otherwise you would get 3 rows each of one lm value,
For that you can try WHERE IN feature of SQL.
SELECT lm from table_b WHERE id IN(ARRAY_OF_IDS)
Or you can also use join to achieve this
Select tale_a.*, tale_b.lm from tale_a inner join table_b ON tale_a.bar=tale_b.id
I assume that you have array of IDs having IDs.
So first make a comma separated string of that array of IDs like this:
ids_str = implode("," $ARRAY_OF_IDS);
and then use that ids_str in IN os mysql query like below:
SELECT lm from table_b WHERE id IN( ids_str )
You can use INNER JOIN
SELECT tale_a.r_id, tale_a.foo, tale_a.bar ,table_b.lm
FROM tale_a
INNER JOIN table_b
ON tale_a.bar=table_b.id
Note: It returns all columns from table_a and only one column from table_b
Resultant Output:
| r_id | foo | bar | lm |
+------+-------+-----+-----+
| 1 | dude | 5 | jk |
+------+-------+-----+-----+
| 2 | homie | 6 |lol |
+------+-------+-----+-----+
| 3 | bro | 7 |funk |
+------+-------+-----+-----+
I have 3 tables, structured like so:
TABLE A:
ID | PCID | ACTIVE | COHORT | WEEKLY_MEETING_TIME | FYE_ID | RC | AGREEMENT_SIGNED | RELEASE_SIGNED | NOTES | FACULTY_ADVISOR
TABLE B:
ID | QUARTER | OFFICE | WRITING_CENTER | etc.. | etc.. | etc.. |
TABLE C:
ID | QUARTER | WEEK | EMAIL | etc.. | etc.. | etc.. |
The common element between all 3 tables is the ID field.
I need to SELECT from all 3 tables, and have each row represent one ID and all the values associated with that ID.
So, for example, each output row should look like a combination of the three tables:
RESULTS:
ID | PCID | ACTIVE | COHORT | WEEKLY_MEETING_TIME | FYE_ID | RC | AGREEMENT_SIGNED | RELEASE_SIGNED | NOTES | FACULTY_ADVISOR | QUARTER | OFFICE | WRITING_CENTER | etc.. | etc.. | etc.. | WEEK | EMAIL | etc.. | etc.. | etc.. |
I have no idea how to structure a query like this. I suspect it involves using JOINs but my attempts have proved futile.
how can I combine the data from 3 tables, based on shared ID field?
SELECT * FROM TABLEA
INNER JOIN TABLEB ON TABLEA.ID = TABLEB.ID
INNER JOIN TABLEC ON TABLEA.ID = TABLEC.ID
If you don't need all the values substitute the "*" with the names of the fields you need (ex. TABLEA.ID, TABLEB.QUARTER, TABLEC.WEEK...)
NATURAL JOIN works in MySQL:
select * from A natural join B natural join C
Try this :
SELECT / WHAT YOU NEED TO SELECT - NOT * !!!/
FROM TABLEA
INNER JOIN TABLEB USING (ID)
INNER JOIN TABLEC USING (ID)
I have a database which (for the purposes of this example), has two tables that have a many to many association (with an intermediary table for holding the associations). Here is there structure:
Table A:
+-----+-------+-------+-------+
| aID | aCol1 | aCol2 | aCol3 |
+-----+-------+-------+-------+
| 1 | foo | aoo | doo |
+-----+-------+-------+-------+
| 2 | bar | aar | dar |
+-----+-------+-------+-------+
| 3 | baz | aaz | daz |
+-----+-------+-------+-------+
Table B:
+-----+-------+
| bID | bCol1 |
+-----+-------+
| 1 | alice |
+-----+-------+
| 2 | bob |
+-----+-------+
Association Table:
+-----+-----+
| aID | bID |
+-----+-----+
| 1 | 1 |
+-----+-----+
| 2 | 2 |
+-----+-----+
| 3 | 1 |
+-----+-----+
If I want to search for information by aCol2 LIKE 'aa%' AND the row has an association to bCol1 = 'bob' (i.e. resulting in only row aID = 2), how could I assemble a MySQL Query that could do something similar?
p.s. Sorry for the poor clarity, I am not exactly sure of the wording, but in a nut shell, it is about searching for data from one record that (for the purposes of this) has a 1-* relationship via a connecting table to a number of records, by information that exists in the entire set
SELECT
a.*
FROM
table_b b
INNER JOIN associations ab ON (b.b_id = ab.b_id)
INNER JOIN table_a a ON (ab.a_id = a.a_id)
WHERE
b.col_1 = 'bob'
AND a.col_2 LIKE 'aa%'
It's been awhile, but I believe this should work:
SELECT
*
FROM
A,
B,
associations
WHERE
A.aCol2 LIKE 'aa%' AND
A.aID = associations.aID AND
associations.bID = B.bID
You have to do 2 inner joins to combine the 3 tables.
I will try to be as explanatory as possible regarding my question. I am using MYSQL/PHP to fetch data from two tables with the structure looking like the following:
table A
+---------+------------+
| userid | username |
+---------+------------+
| 1 | john |
| 2 | doe |
| 3 | lewis |
+---------+------------+
table B
+---------+------------+-----------+
| id |from_userid | to_userid |
+---------+------------+-----------+
| 1 | 1 | 3 |
| 2 | 3 | 2 |
| 3 | 1 | 2 |
| 4 | 2 | 1 |
+---------+------------+-----------+
Am trying to achieve the following:
+---------+------------+----------------------+
| id |sender username| receiver username |
+---------+------------+----------------------+
| 1 | john | lewis |
| 2 | lewis | doe |
| 3 | john | doe |
| 4 | doe | john |
+---------+------------+----------------------+
As you can see, instead of returning the sender or receiver user id, I am returning their username according to Table A.
can I use left or right joins in this scenario? Thanks in advance
Try this
SELECT b.id, sender.username AS sender_username, receiver.username AS receiver_username
FROM tableB AS b
JOIN tableA AS sender ON b.from_userid = sender.userid
JOIN tableA AS receiver ON b.to_userid = receiver.userid
Inner joins would work fine, you just need to do two of them...
Left and right joins are only needed when you want to get all records from one table and some from another table. In this case you have two distinct relationships thus the need for two joins. My MySQL skills are a bit rusty so I don't know if ' or [ are used as separators on the table/field names with spaces.
Select t1.ID, T1.userName as 'Sender userName', T2.username as 'Receiver username'
FROM [Table A] A
INNER JOIN [Table B] T1
on T1.from_userid = A.userID
INNER JOIN [Table B] T2
on T2.to_userid = A.userID
SELECT move.idItem, item.description,
(
SELECT location.location_name as movedFrom FROM move, location
WHERE move.idlocationFrom = location.idlocation
) AS movedFrom,
(
SELECT location.location_name as movedTo FROM move, location
WHERE move.idlocationTo = location.idlocation
) AS movedTo
FROM move , item
WHERE move.idItem = item.idItem
I'm trying to get the name of the location movedTo and movedFrom using the above query, which produdes '#1242 - Subquery returns more than 1 row' error in PHPMyAdmin.
Description
Item movements are stored in a table called 'move'. Items can be moved from one location to another storing the location id's (location names stored in 'location' table) and item names stored in 'item' table. movedTo and movedFrom will store the id's from the location table.
Can someone please help me with this query?
Thanks in advance
I think you might want to use joins instead subqueries. Something like:
SELECT move.idItem,
item.description,
location1.location_name movedFrom,
location2.location_name as movedTo
FROM move
INNER JOIN location location1 ON move.idlocationFrom = locatio1n.idlocation
INNER JOIN location location2 ON move.idlocationTo = location2.idlocation
INNER JOIN item ON move.idItem = item.idItem
Table 1 - cpe Table
|id | name
|----------
| 1 | cat
| 2 | dog
| 3 | mouse
| 4 | snake
-----------
Table 2 - AutoSelect
|id | name | cpe1_id | cpe2_id | cpe3_id |
|-----------------------------------------------
| 1 | user1 | 1 | 3 | 4 |
| 2 | user2 | 3 | 1 | 2 |
| 3 | user3 | 3 | 3 | 2 |
| 4 | user4 | 4 | 2 | 1 |
------------------------------------------------
I would like to see an output of
user1 | cat | mouse | snake |
user2 | mouse | snake | dog |
..etc
SELECT a.name, cpe1.name, cpe2.name, cpe3.name FROM AutoSelect as a
LEFT JOIN cpe as cpe1 ON ( cpe1.id = a.cpe1_id )
LEFT JOIN cpe as cpe2 ON ( cpe2.id = a.cpe2_id )
LEFT JOIN cpe as cpe3 ON ( cpe3.id = a.cpe3_id )