I have these two tables which I'm trying to make an output of.
The first one **USERS** stores all information about a user, including an unique ID (androidID).
The second one gets input based on number of laps a user has taken, and will make one row pr lap.
What I'm trying to do is to output the last entry of a given androidID in **ROUNDS**, whith the corresponding name etc. from **USERS**
_____________________ _________________________
| **USERS** | | **ROUNDS** |
--------------------- -------------------------
| NAME | | ID(unique) |
| LASTNAME | | TIME |
| androidID(unique) | <----> | androidID(NOT unique) |
| ... | | ROUNDS |
This is how I'm quering the server
$result_users = $con->query(
"SELECT * FROM users"
);
$result_rounds = $con->query(
"SELECT * FROM Rounds ORDER BY laps desc"
);
I tried to numerous combination of the following. With no luck. My PHP skills is not the best, I'm afraid.
foreach ($result_users as $row_users) {
foreach ($result_rounds as $row_rounds) {
if($row_users['androidID'] == $row_rounds['androidID'] {
// Do some wizzardy
}
}
}
I have really hit a wall trying to connect the tables.
This would be the sql statement you want in your query.
SELECT * FROM `**USERS**` LEFT JOIN `**ROUNDS**`ON `**USERS**`.`androidID` = `**rounds**`.`androidID` ORDER BY `laps` desc 0,1;
Related
My job is to cleanse the database from broken store records(records with missing data like a store name or streetname, database example below). However it turns out some of those records have employees linked to them so we can't just delete them.
Question: what kind of query do I need to make sure only the employees of those stores are shown?
I have three tables in which i need to work: user, department and store_users (store_users is a table in which all ID's are packed together to see which user_id belongs to which store_id.)
Example of code
its most likely wrong but its to give some insight in what I am trying to accomplish.(its purely mysql at the moment)
SELECT
*
FROM
gebruiker
WHERE
(SELECT
*
FROM
winkel_adresgegevens
WHERE
winkel_id IN (SELECT
winkel_gebruikers_winkel_id
FROM
winkel_gebruikers)
AND (winkel_naam = '' OR winkel_straat = ''
OR winkel_huisnummer = ''
OR winkel_postcode = ''
OR winkel_plaats = ''));
database example
+--------+---------------+----------+------------+------------------+
|store_id|store_branch_id|store_name|store_street|store_streetnumber|
|-------------------------------------------------------------------|
| 1 | 100 | Jumbo | Hilversum | 14 |
|--------+---------------+----------+------------+------------------|
| 2 | 150 | Lidl | Kerkelanden| 24 |
|--------+---------------+----------+------------+------------------|
| 3 | 105 | | Loosdrecht | |
|--------+---------------+----------+------------+------------------|
| 4 | 200 | Coop | | 14 |
+--------+---------------+----------+------------+------------------+
hence the question: only show employees from the broken records. (the ones with missing data, see store_name, store_street and store_streetnumber)
UPDATE : This is what you want
SELECT *
FROM employees
WHERE store_name IS NULL OR store_street IS NULL OR store_streetnumber IS NULL ;
So i will show my sample of table so that it easier to visualize the situation. This is program table.
ProgID | ProgramName | SendNotifyToEmail
-------------------------------------------------------------------------------
1. | A | a#gmail.com, b#gmail.com, c#gmail.com, d#gmail.com
-------------------------------------------------------------------------------
2. | B | a#gmail.com, b#gmail.com, c#gmail.com
-------------------------------------------------------------------------------
3. | C | a#gmail.com, b#gmail.com, c#gmail.com
--------------------------------------------------------------------------------
4. | D | e#gmail.com, f#gmail.com, g#gmail.com
--------------------------------------------------------------------------------
5. | E | d#gmail.com, e#gmail.com, f#gmail.com
---------------------------------------------------------------------------------
6. | F | b#gmail.com, e#gmail, f#gmail
---------------------------------------------------------------------------------
so i try to SELECT
SELECT * from program where SendNotifyToEmail like '%d#gmail.com%'
and wanted to apply an update to the related value.
how to apply an update to all d#gmail.com to j#gmail.com using explode for each.
$query ="SELECT * from program where SendNotifyToEmail like '%d#gmail.com%'";
$result = mysql_query($query);
while($line=mysql_fetch_array($result, MYSQL_BOTH))
{
$listmail = explode(",", trim($line["SendNotifyToEmail"]).',');
foreach($listmail as $email=>$i)
{
////
}
}
If it's not an often done procedure or if your database is small enough you may do it with a single query.
UPDATE program SET SendNotifyToEmail = REPLACE(SendNotifyToEmail, 'd#gmail.com', 'j#gmail.com');
For me is solution to change database structure. SendNotifyToEmail should contain id of email saved in separate table, or you can have table with ProgramName, table with emails and join table.
You can use two tables and
Link them together with a foreign key
first table :ProgID | ProgramName |
second table: EmailId | Prog_ForignId | SendNotifyToEmail
In my table 'users' there are 'friends' ,
Like this :
+----+------+---------+
| id | name | friends |
+----+------+---------+
| 1 | a | 0,1,2 |
| 2 | b | 0,1,3 |
| 3 | c | 0,1 |
+----+------+---------+
How do I use the explode function to get the friends id one by one (not 0,1,2) that are separated by a comma (,) ;
How do I select the id? (Example) :
$sql = Select id from users where id = (exploded)
if (mysql_num_rows($sql) > 0 ) {
$TPL->addbutton('Unfriend');
}else{
$TPL->addbutton('Add as Friend')
}
The solution here is actually a slight change in your database structure. I recommend you create a "many-to-many" relational table containing all of the users friends referenced by user.
+---------+-----------+
| user_id | firend_id |
+---------+-----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 5 |
+---------+-----------+
If you are storing lists of values within one field then that is the first sign that your database design is not quite optimal. If you need to search for a numerical value, it'll always be better to place an index on that field to increase efficiency and make the database work for you and not the other way around :)
Then to find out if a user is a friend of someone, you'll query this table -
SELECT * FROM users_friends WHERE
`user_id` = CURRENT_USER AND `friend_id` = OTHER_USER
To get all the friends of a certain user you would do this -
SELECT * FROM users_friends WHERE `user_id` = CURRENT_USER
Just a simple example that will make you clear how to proceed:
// Obtain an array of single values from data like "1,2,3"...
$friends = explode(',', $row['friends']);
Then, back in your query:
// Obtain back data like "1,2,3" from an array of single values...
$frieldslist = implode(',', $friends);
$sql = "SELECT * FROM users WHERE id IN ('" . $frieldslist . "')";
to get an array of if ids from your string explode would be used like this
$my_array = explode("," , $friends);
but you'd probably be better using the mysql IN clause
$sql = "Select id from users where id in (".$row['friends'].")";
Just a quick idea. Change your database's table. It is certain that after a while many problems will arise.
You could have something like this.
id hasfriend
1 2
1 3
2 1 no need to be here (You have this already)
2 4
.....
You can do this by using indexes for uniqueness or programming. You may think of something better. Change your approach to the problem to something like this.
I have one field in the backend, where I input IDs separated by comma - ID1, ID2, ID3....These are videos in fact. All ids are stored in the field product_videos in the database (as they are typed).
How can I echo these id's on the frontend so they all show for the product?
Storing comma separated data in one data field is a bad idea. It is a real pain to manipulate, so you should really consider revising your db structure.
You haven't shown your data structure, so I'll give a basic example and then explain how it can be improved. My example assumes product_videos is linked to particular users:
table: `users`
| user_id | user_name | product_videos |
|---------|-----------|----------------|
| 1 | User1 | 1,2,3,4,6,7 |
| 2 | User2 | 5 |
You would maybe run a query
SELECT `product_videos` FROM `users` WHERE `user_name` = 'User1'
This would give you one row, with a comma separate value - you would then need to use something like PHP's explode() to convert it into an array and then loop through that array. That is a very bad method (and it will only become harder as you try to do more advanced things).
Instead, it would be easier to use a link table. Imagine:
table: `users`
| user_id | user_name |
|---------|-----------|
| 1 | User1 |
| 2 | User2 |
table: `videos`
| video_id | user_id |
|-----------|---------|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
| 6 | 1 |
| 7 | 1 |
In this example, each video is a separate row in a db table (and each video is linked to an existing user). Each row is readily able to be handled independently. This makes it really easy to handle extra data for each video, such as a title, runtime length, date of uploading, etc.
You would then need to run a JOIN query. e.g.
SELECT `videos`.`video_id` FROM `videos`
INNER JOIN `users` ON `users`.`user_id` = `videos`.`user_id`
WHERE `users`.`user_name` = 'User1'
In PHP, you would do something like:
$q = mysql_query("SELECT `videos`.`video_id` FROM `videos` INNER JOIN `users` ON `users`.`user_id` = `videos`.`user_id` WHERE `users`.`user_name` = 'User1'");
while ($row = mysql_fetch_assoc($q)) {
echo "VIDEO ID = " . $row["video_id"] . "<br/>";
}
I am developing an android Application that stimulates a poll notification feature. Therefore, I Created a service that keeps asking the server for the new data. However, I've used a table in my database called Seen. This table is used to be asked by the server for a specific user_id and if the news_id didn't exists in that table it will produce a notification.
The problem is when i launch the application for the first time. It retrieves all the data from the database because the server considering the users didn't see the news.
If Anyone Can Help me?
I Thought to solve it by this Idea: when I launch the application for the first time. Insert in seen table all of the news with that user_id in order to get 0 new messages. But i think it will be Not efficient.
This is my Database and my PHP script
Users table
User_ID | User_Name
--------------------
1 | John
2 | Carl
3 | Tomas
4 | Adam
5 | Nancy
News Table
News_ID | News_Text | news_date
---------------------------
1 | Hello World | CURRENTDATE()
2 | This is My car | CURRENTDATE()
3 | I had Ate pizza| CURRENTDATE()
4 | Leave Me Alone | CURRENTDATE()
5 | C++ Programming| CURRENTDATE()
Seen Table
ID | User_Id | News_Id
---------------------------
1 | 1 | 2
2 | 1 | 3
3 | 4 | 1
4 | 5 | 3
5 | 1 | 4
This is my PHP Code and it also showing my Query to get the news that didn't show in the Seen_news Table :
<?php
require('config.php');
$conn = mysqli_connect($servername, $username, $password, $db);
$query="SELECT * FROM news WHERE news_id NOT IN (SELECT news_id FROM news_seen WHERE user_id = '".$_GET['id']."')";
$result = mysqli_query($conn,$query);
$rows = array();
echo mysqli_error($conn);
while($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
echo json_encode($rows);
?>
Supposing that i am sending the User_Id to the PHP script and based on the result Query will show json file.
If you can add a create_date column to your Users table, you can select where Seen news_date is greater than User create_date.
Something like:
"SELECT * FROM news WHERE news_id NOT IN (SELECT news_id FROM news_seen WHERE user_id = '".$_GET['id']."') AND news_date > (SELECT create_date FROM user WHERE user_id = '".$_GET['id']."')"