I'm trying to achieve a PHP, SQL and HTML solution. This is probably commonly done.
I have coded a project overview system from scratch. The state right now is that I have a SQL table for USERS and another table for PROJECTS. It works great to sign in with each user and also to list all projects for all the users.
But to be able to track which users that are connected to each project I created a third SQL table which is called PROJECTMEMBERS. It contains the rows USERS_ID and PROJECTS_ID which are supposed to hold the ID from the user and the project to be able to track the projectmembers. I'm not sure if this is the best solution but found it in another thread and thought it made sense. But now..
I have issues to figure out how to access different SQL tables in the same mysqli_fetch() query. How is this possibly done to present the USER IMAGE in each project? The current fetch is used to present all the projects but I still want to show all projects, but with an image of the active users on the project on each.
I hope I was clear enough for anyone to understand. Looking forward to any help out there.
SQL STRUCTURE EXAMPLE:
tbl_USERS tbl_PROJECTS
| id | username | USER_IMG | etc | | id | PROJECTNAME | DATE | etc |
---------------------------------- ----------------------------------
| 1 | XXXX | XXXXXXXX | 1 | | 1 | AAAA | AAAA | 1 |
| 2 | YYYY | YYYYYYYY | 2 | | 2 | BBBB | BBBB | 3 |
| 3 | ZZZZ | ZZZZZZZZ | 1 | | 3 | CCCC | CCCC | 4 |
tbl_PROJECTMEMBERS
| USERS_ID | PROJECTS_ID |
-------------------------
| 1 | 1 |
| 1 | 5 |
| 2 | 1 |
How it should be presented where USER_IMG is fetched from the users table:
-------------------------------- --------------------------------
| PROJECTNAME | | PROJECTNAME |
| DATE | | DATE |
| | | |
| | | |
| USER_IMG, USER_IMG | | USER_IMG, USER_IMG |
-------------------------------- --------------------------------
--------------------------------
| PROJECTNAME |
| DATE |
| |
| |
| USER_IMG |
--------------------------------
As you have 3 table users and projects and user_projects and you are fetching only projects from projects table then you can add join the first query where you fetch the projects it could some like this
SELECT p.,user. FROM projects as p
INNER JOIN user_projects as up
ON up.project_id = p.id
INNER JOIN users
ON users.id = up.user_id
Above I have used multijoin where project table connected with user_project and user_project conned with users table and in the select query we have fetch project and users table.
In above query you might get query if same attribute get repate into the both table like (ambiguity) so you can just skip the error just by alise it
SELECT user.id AS 'User ID', project.id as 'Project ID', project.name as 'Project Name' from user tbl_USERS , project tbl_PROJECTS, userprojects tbl_PROJECTMEMBERS where user.id = userprojects.USERS_ID AND project.id = userprojects.PROJECT_ID
This would be your mysql query, it would fetch the user id as User ID, project Id as Project ID, and project name as Project Name.
You can access those variables in plain php and output anyway you like.
Something like this?
$query = "SELECT a.*, b.* FROM USERS a INNER JOIN PROJECTS b on a.userID = b.userID
RIGHT JOIN USER_PROJECTS c ON a.userID = c.userID
WHERE a.userID = $variable";
$result = $db->Execute($query)
foreach($result as $data){
if($data['userID'] == $data['projectID']){
//do stuff here
}
}
Related
I need some help with MySQL/PHP. Which is the faster execution in code point of view.
I have below tables
1. table_content
--------------------------------
id | section | content_id
--------------------------------
1 | A | 15
2 | B | 25
3 | A | 9
--------------------------------
2. table_a
--------------------------------
id | name | message
--------------------------------
9 | John | Hello Everyone
15 | Smita | Hi
17 | Vinayak | How are you?
--------------------------------
3. table_b
--------------------------------
id | label | description
--------------------------------
1 | David | D1
5 | Alia | D2
25 | Vinay | D3
--------------------------------
I have above table structure. For me table_content is main table. I want below output through MySQL/PHP [As array and section as key].
Output
------------------------------------------------
id | section | name | message
------------------------------------------------
1 | A | Smita | Hi
2 | B | Vinay | D3
3 | A | John | Hello Everyone
------------------------------------------------
I have tried with SWITCH case. But not getting exact output.
Which is the better performance and fast execution? with MySQL or PHP. I have thousands of data like this.
Please help me to solve this problem.
Should be acheved via a JOIN.
Also ensure you have configured appropriater indexing, otherwise it will perform badly when you get to a large volume of data.
As i posted in my comment you need to Join the both tables, which must be UNION
SELECT tc.`id`, tc. `section` ,t1.`message`
FROM table_content tc JOIN (SELECT `id`, `name`, `message` FROM table_a UNION SELECT `id`, `label`, `description` FROM table_b) t1
ON tc.`content_id` = t1.`id`
id | section | message
-: | :------ | :-------------
1 | A | Hi
2 | B | D3
3 | A | Hello Everyone
db<>fiddle here
My question is: Can I do this?
I try many things, some of them are:
Search 1
Search 2
Search 3
Search 4
I need all the info form Table A, and sometimes I need to join this table with Table B. My problem is that when I join both tables, if an specific parameter in Table A is not in Table B just give me the records when the specific parameter are, but I want all the records.
Table A
|--------------------------------|
| Table A |
|-------------------|------------|
| id_table_A | name | id_table_B |
|------------|------|------------|
| 1 | Joe | 1 |
|------------|------|------------|
| 2 | Ben | |
|------------|------|------------|
| 3 | Lya | |
|------------|------|------------|
| 4 | luis | 2 |
|------------|------|------------|
Table B
|----------------------|
| Table B |
|----------------------|
| id_table_B | Elements|
|------------|---------|
| 1 | Car |
|------------|---------|
| 2 | Byke |
|------------|---------|
| 3 | Moto |
|------------|---------|
What I want to show in my View is this:
|------------|------|------------|
| id_table_A | name | Elements |
|------------|------|------------|
| 1 | Joe | Car |
|------------|------|------------|
| 2 | Ben | |
|------------|------|------------|
| 3 | Lya | |
|------------|------|------------|
| 4 | luis | Byke |
|------------|------|------------|
My model
In my model this is what I tried:
"SELECT * FROM table_A, table_B where table_A.id_table_B = table_B.id_table_B"
But this query only show me data 1 and 4.
This can be done or not?
Thanks in advance.
You can use left join
SELECT *
FROM table_A
left join table_B on table_A.id_table_B = table_B.id_table_B
Left join is used when the keys between tables may not always match. In this case, the left join retrieves the correct match where it's possible and the values become NULL when not possible.
SQL LEFT JOIN Documentation
You need an explicit LEFT JOIN as opposed to the implicit INNER JOIN that is used when you simply list the tables like that. https://www.w3schools.com/sql/sql_join_left.asp
I have a table called photos in a MySQL database that contains photo filenames and other related info. Structure as follows:
+----------+------------+-------------+
| photo_id | gallery_id | photo_fname |
|----------|------------|-------------|
| 1 | 1 | DSC2001 |
| 2 | 1 | DSC2002 |
| 3 | 1 | DSC2003 |
| 4 | 2 | DSC2004 |
| 5 | 2 | DSC2005 |
| 6 | 2 | DSC2006 |
| 7 | 3 | DSC2007 |
| 8 | 3 | DSC2008 |
| 9 | 3 | DSC2009 |
+----------+------------+-------------+
What I want to do is write a query that returns the first photo filename for each gallery, as follows:
+----------+------------+-------------+
| photo_id | gallery_id | photo_fname |
|----------|------------|-------------|
| 1 | 1 | DSC2001 |
| 4 | 2 | DSC2004 |
| 7 | 3 | DSC2007 |
+----------+------------+-------------+
I researched this until my headache overcame me last night and although I found similar questions, they didn't seem to me to be looking to do the exact same thing. I tried subqueries, I tried the sample code from other posts here but couldn't make it work. Either due to brain fog or just not finding the right solution.
I just want to return the results as shown above, nothing more.
Many thanks in advance!
UPDATE:
My apologies, the table name is photos, FWIW.
Secondly, all I really want is the filename, not the photo or gallery Id, I just included those for visual clarity. I apologize if that caused confusion.
Try this:
SELECT *
FROM YourTable
WHERE photo_id IN ( SELECT MIN(photo_id)
FROM YourTable
GROUP BY gallery_id)
See a demo
For your updated question:
SELECT photo_fname
FROM photos
WHERE photo_id IN ( SELECT MIN(photo_id)
FROM photos
GROUP BY gallery_id)
Here is an easy way using inner join
SELECT
m.*
FROM sparkles as m
INNER JOIN (SELECT
photo_id,
MIN(photo_id)
FROM sparkles
GROUP BY gallery_id) as l
on l.photo_id = m.photo_id
Demo
SELECT photo_id, gallery_id, photo_frame
FROM ...
GROUP BY photo_id, gallery_id
by default, mysql will return the FIRST non-grouped value it encounters in the DB for any non-aggregated fields in the field list.
Select * from tablename group by gallery_id; ?
I have a few tables in a MySQL database similar to this setup:
major table
---------------------
| id | name |
|-------------------|
| 0 | Architecture |
| 1 | Biology |
| 2 | Chemistry |
---------------------
college table
----------------------
| id | name |
|--------------------|
| 0 | Georgia Tech |
| 1 | Virginia Tech |
| 2 | Cal Tech |
----------------------
users table
----------------------------------------------
| id | name | major_id | college_id |
|--------------------------------------------|
| 0 | John Smith | 2 | 0 |
| 1 | Kevin Lee | 2 | 1 |
| 2 | Matt Anderson | 0 | 2 |
----------------------------------------------
Using PHP, I want to get all the information for a user using a query similar to this:
SELECT * FROM users WHERE name=`$user`
Is there someway for MySQL to automatically link the "major_id" and "college_id" columns to the "major" and "college" tables in a way where the query above would return the appropriate values?
If it is not possible with a single query, would multiple queries slow down performance considerably?
SELECT * FROM users WHERE name='$user'
This query (yours has back ticks around $user, back ticks are for column names, use double/single quotes) will only return values from the users table. You can't make MySQL "automagically" construct your joins. You have to do it explicitly, otherwise, how would you get information only from the users table if you wanted to? Use a JOIN like this:
SELECT users.name AS Username, college.name AS College, major.name AS Major
FROM users
INNER JOIN college ON users.college_id = college.id
INNER JOIN major ON users.major_id = major.id
Limit the retrieved columns by only selecting the ones you really need. So instead of the asterisk, write users.name etc.
The JOIN syntax is described in the MySQL Docs.
Joins are what your looking for, in this case your SQL would be:
SELECT `users`.`name`, `major`.`name`, `college`.`name`
FROM `users` WHERE `users`.`name`='name'
INNER JOIN `major` ON `major`.`id`=`users`.`major_id`
INNER JOIN `college` ON `college`.`id`=`users`.`college_id`
You can also alias your field names so you get something a bit more usable out:
SELECT `users`.`name` AS `applicant_name`, `major`.`name` AS `major_name`, `college`.`name` AS `college_name`
FROM `users` WHERE `users`.`name`='name'
INNER JOIN `major` ON `major`.`id`=`users`.`major_id`
INNER JOIN `college` ON `college`.`id`=`users`.`college_id`
More on Joins at http://dev.mysql.com/doc/refman/5.0/en/join.html
I have two tables a and b as follows to implement a simple block list where users can block other users.....
Table A
+------------+--------------+------+
| Name | phone |userid|
+------------+--------------+------+
| Mr Sasi | 01225 708225 | 1 |
| Miss Brown | 01225 899360 | 2 |
| Mr Black | 01380 724040 | 3 |
+------------+--------------+------+
Table B
+------------+--------------+
| blockedbyid| blockedid |
+------------+--------------+
| 1 | 2 |
| 2 | 3 |
| 1 | 3 |
+------------+--------------+
"blockedbyid" is id of user who has blocked the user in "blockedid".
I need to join the two tables and fetch all records from table A such that the result has all users who are not blocked by a particular user [ie blockedbyid='XXX'].. Can you guys give the SQL query so that i can fetch the records as a recordset??? I dont want to fetch two different rowsets and compare it in php....
Something like this should work
Parameter :USERID
SELECT * FROM TABLEA WHERE userid NOT IN (SELECT blockedid FROM TABLEB WHERE blockedbyid = :USERID)
Using join
SELECT u.* FROM TABLEB b, TABLEA u WHERE b.blockedbyid = 'XXX' AND b.blockedid = NULL
It may work like that, give it a try.
Roadie57 solutions seems better though.