I have search but i dont solve my problems.
I have a many to many relationship.
3 tables:
groups_mail | groups_mail_j (joint table) | mails
id, name | mail_id, groupe_id | id, name
I dont know how select all the emails for my groupe ID=1
Thx.
Your question is not clear. Anyways this is what I have understood based on the question.
Your having 3 tables mails, groups_mail, groups_mail_j
You want to join all the tables and get all the records based on mails-> id column.
If above is the required on than you can do that by the following
SELECT m.id mailid, m.name AS email, gm.name AS groupname, gm.id AS groupmailid
FROM mails AS m
INNER JOIN groups_mail_j AS gmj ON gmj.mail_id = m.id
INNER JOIN groups_mail AS gm ON gm.id = gmj.groupe_id
WHERE m.id = 1
Here I have used the custom selectors. You can use any selector ie specific column of tables by using the alias of that table.
Okay so in-case you want to show all the mail-ids for the selected group then use the following query. No need to put the mail_id because if you knew mail id then you wouldn't have to query again.
SELECT m.name AS email
FROM mails AS m
INNER JOIN groups_mail_j AS gmj ON gmj.mail_id = m.id
INNER JOIN groups_mail AS gm ON gm.id = gmj.groupe_id
WHERE gmj.groupe_id = 1
Here 1 in hard coded in case you can user $group_id
Please check your spelling whether is group_id or groupe_id
Related
I have a form with radio buttons which stores the value (which is the ID) in my MySQL database along with the necessary information from the user.
INSERT INTO table (user_id, name, address, prefer_id) VALUES ('', ?, ?, ?);
So when I try to fetch the data, I use LEFT JOIN, to get the necessary description from table2:
SELECT a.name, a.address, b.prefer_desc FROM table a
LEFT JOIN table2 b ON a.prefer_id = b.prefer_id
WHERE a.user_id = ?
But I have created an other option, in case the option the user prefers is not in the list. A textbox will appear when the user selects Other in the list of radio buttons so they can type-in freely their preferred data. Check this fiddle to see an example.
The first logic that I've thinked of is to create a separate table which stores the typed-in data of the user.
other_tb:
other_id | user_id | typed_in |
----------+---------+----------+
1 | 1 | cake |
2 | 3 | pizza |
So when I fetch the data, I use php's if() condition if the prefer_id is 4 (or other), and if it does, I will use another SELECT query to get the other data in other_tb table.
SELECT typed_in FROM other_tb WHERE user_id = ?
Is there a way to do all of this in a single query?
OR
Is this the best option, or is there a right or better way in this kind of situation?
try this
SELECT
a.name,
a.address,
IF(a.prefer_id=4,(SELECT typed_in FROM other_tb WHERE user_id = a.user_id),b.prefer_desc) as prefer_desc
FROM table a
LEFT JOIN table2 b
ON a.prefer_id = b.prefer_id
WHERE a.user_id = ?
If you are using the second table as you described, and you want to keep the same format of the output and assuming the records in other_tb are unique for each user_id, you could use something like this:
SELECT a.name, a.address,
CASE
WHERE a.prefer_id = 4 THEN c.typed_in
ELSE b.prefer_desc
END CASE AS prefer_desc
FROM table a
LEFT JOIN table2 b ON a.prefer_id = b.prefer_id
LEFT JOIN other_tb c ON a.user_id = c.user_id
WHERE a.user_id = ?
I think this can also be written as:
SELECT a.name, a.address,
CASE a.prefer_id
WHERE 4 THEN c.typed_in
ELSE b.prefer_desc
END CASE AS prefer_desc
FROM table a
LEFT JOIN table2 b ON a.prefer_id = b.prefer_id
LEFT JOIN other_tb c ON a.user_id = c.user_id
WHERE a.user_id = ?
There are two approaches that I was able to practice depending on the situation:
Allow users to add more options so that their answer is still indexed to table2. So when fetching data, you may still use LEFT JOIN to get the user's preferred option without any other condition. But this method will allow users to see the options that was added by other users.
If the options are "fixed" and only the administrator is allowed to add options to table2, have another column to table1, which stores the other answer of the user (let say other_option column). This method allows the user to still freely state their option without adding options to table2.
Here is table1:
+---------+------+---------+-----------+--------------+
| user_id | name | address | prefer_id | other_option |
+---------+------+---------+-----------+--------------+
What I do is I put 0 to prefer_id if the user prefers to answer other_option. So your program expects that if the prefer_id is 0, you have an input in other_option.
I have a SELECT statement that pulls data from one table:
SELECT user_id, user_email, product_id, download_count
FROM l3x_woocommerce_downloadable_product_permissions
but using the user_id I need to find the group_id from another table
and using the group_id then get the group name (name) from a third table.
so have this:
SELECT r1.user_id, r1.user_email, r1.product_id, r1.download_count, r2.group_id, r3.name
FROM l3x_woocommerce_downloadable_product_permissions r1
, l3x_groups_user_group r2
, l3x_groups_group r3
but i dont think the 2nd and 3rd table data is usable as it is not associated with user id.
How do i join (JOIN?) the 3 tables together to get group name?
if the tables are set up so that r2 contains user_id and associated group_id and r3 is set up similarly you can
use join
SELECT r1.user_id, r1.user_email, r1.product_id, r1.download_count, r2.group_id, r3.name, r3.group_name
FROM l3x_woocommerce_downloadable_product_permissions r1
JOIN l3x_groups_user_group r2
ON r1.user_id = r2.user_id
JOIN l3x_groups_group r3
ON r2.group_id = r3.group_name
You'll have to pick a field in each table that is shared.
I'd assume r1.group_id=r2.group_id and r2.name=r3.name. In your case, the fields in r1/r2 may have different names than in r2/r3s. You also might have to join using a different field than these. I don't know without knowing all the fields in each table.
You'll then JOIN ON those common fields.
So you'll use somthing like
SELECT r1.user_id, r1.user_email, r1.product_id, r1.download_count, r2.group_id, r3.name
FROM ((l3x_woocommerce_downloadable_product_permissions r1
LEFT JOIN l3x_groups_user_group r2 ON r1.group_id=r2.group_id)
LEFT JOIN l3x_groups_group r3 ON r2.name=r3.name)
If you give a list of the fields in all 3 tables, I can give a more specific answer, but this is the form of the query you need.
Thank you for all your help. This seems to be working:
SELECT r1.user_id, r1.product_id, r1.download_count, r2.group_id, r3.name
FROM l3x_woocommerce_downloadable_product_permissions r1
JOIN l3x_groups_user_group r2 ON r1.user_id = r2.user_id
JOIN l3x_groups_group r3 ON r2.group_id = r3.group_id
WHERE r1.download_count > 0 AND r2.group_id != 1
I have two mysql tables members_tbl and post_tbl
members_tbl:
id|userName |fname |lname |friendArray
post_tbl:
postId| memId | thePost |postDate
now, I'm trying to display post from user id and from his friendArray.
please let me know how to do it (still new to php)
Since MySQL lacks an explode function, you either need to create a relation table and use joins, or use multiple queries with php processing inbetween. I strongly recommend the relational approach as it conforms to database standards (normalization) much more than the alternative and is easier to implement.
You need a third table, which describes the relation between two friends, I.E.
friends_tbl
user1_id | user2_id
With a primary key on user1_id and user2_id (thereby preventing duplicates). For every friend relationship, I.E. user 1 is friends with user 2, there is one row in this table. You can then get the listing you want with the following query.
SELECT p.*, u.*
FROM posts_tbl p
INNER JOIN members_tbl u
ON u.id = p.memId
WHERE u.id IN (
SELECT user2_id AS id
FROM friends_tbl
INNER JOIN members_tbl
ON (user1_id = id)
WHERE members_tbl.id = $id
UNION
SELECT user1_id AS id
FROM friends_tbl
INNER JOIN members_tbl
ON (user2_id = id)
WHERE members_tbl.id = $id
)
ORDER BY p.postDate
SQLFiddle of the above.
create a different table for your friend-relations and then join this table in your SQL.
i have an Table with Users and an Table with events that is related to the id from the Users Table. In each Event you can make points and i want to get the total points from an specific User.
It looks like this:
Users
+----+----------+
| id | username |
+----+----------+
Events
+----------+---------+--------+
| event_id | user_id | points |
+----------+---------+--------+
// The event_id is related to an Event Table with specific data about the Event. but that not relevant.
The best could be to get the data from the user and the total points that he got in one query.
Thanks and Greetings,
Mottenmann
"..to get the data from the user and the total points that he got in one query."
You need to join both tables first so you can manipulate the data. The query below uses INNER JOIN which only includes users on the result list if it has atleast one matching record on the Events. If you want to get all users even without a single matching record on the other table, use LEFT JOIN instead.
SELECT a.ID, a.username, SUM(b.points) totalPoints
FROM Users a
INNER JOIN Events b
ON a.ID = b.user_ID
GROUP BY a.ID, a.username
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
"In each Event you can make points and i want to get the total points
from a specific User."
You could do something like this:
select sum(e.points) as points from users u
left join events e ON (u.id = e.user_id)
WHERE u.id = {$id}
where {$id} is the id of user.
I am having a problem writing a MySQL query to get data from 2 tables in the same database. I don't know how to combine the 2 tables into 1 table basically so that I can use PHP to display the information. Right now I am using 2 queries and PHP to join them... I really need the persons name and the score they got.
tbl_scores
----------
user_id
points
tbl_users
---------
user_id
name
tbl_users has more fields but those are the only ones that matter I think? Please help or send what I need to look up to learn more.
SELECT * FROM tbl_scores RIGHT JOIN tbl_users ON tbl_scores.user_id = tbl_users.user_id;
The word you're looking to use is JOIN... more information on MySQL JOINs can be found all over the internet but I found one here: http://mysqljoin.com/
The following code is untested but should work for your scenario...
SELECT
u.name,
s.points
FROM
tbl_users AS u LEFT JOIN tbl_scores AS s ON u.user_id = s.user_id
use Join and more specificaly Left Join because it returns all rows from the left table (tbl_users ), even if there are no matches in the right table (tbl_scores) so that you get all users even those with no score :
SELECT * FROM tbl_users LEFT JOIN tbl_scores ON
tbl_users.user_id = tbl_scores.user_id;