I have two tables.
First tabel called:(data):
----------------------------------------
id link number isik status
----------------------------------------
1 /link 78788 56677 55
Second table called:(test)
----------------------------------------
id kood status
----------------------------------------
1 56677 111
The only similar thing in two tables are the isik and kood
How can I get all the rows from the First table where isik(First table) = kood(Second table)?
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT data * FROM data INNER JOIN test ON data.isik = test.kood";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Numb: " . $row["number"]. " - Name: " . $row["isik"]. " " . $row["link"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
But im getting 0 results
You can join the tables together using an inner join and select only rows from the first table. This will select only rows where the second table has a matching value for isik.
SELECT `data`.* FROM `data` INNER JOIN `test` ON `data`.`isik` = `test`.`kood`
SELECT *
FROM First a
WHERE EXISTS (SELECT 1
FROM Second b
WHERE a.isik = b.isik);
Related
So I have this table 'users_photos'. It contains 38k rows with user pictures. Every row contains id, userid and link to the photo. So if a user have 3 pictures, that user id will show in 3 rows in the database.
What I want to do is count the number of users with 1 picture in the database, 2 pictures in the database etc.
UPDATE: I have now the following code
$sql = $mysqli->query("SELECT count(*), count_users from (SELECT u_id, count(*) as count_users FROM users_photos group by u_id) temp group by count_users");
$sql->data_seek(0);
while ($row = $sql->fetch_assoc()) {
echo "".$fetch." = " . $row['count_users'] . "\n<br>";
}
This prints the users that have 1 picture and up to 8. Not how many but only shows that in the database there is users that have 1 picture, 2 pictures etc. Now I need to figure out how to print the total users that have 1 picture etc.
Anyone have any tips? thanks on behalf!
Update Your Query With This
$sql = $mysqli->query("SELECT count(*),u_id as 'count_users' FROM users_photos group by u_id");
Sql Query:
$sql = $mysqli->query("SELECT count(*),u_id as 'count_users' FROM users_photos group by u_id");
You Can Print like this
// After Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT count(*),u_id as 'count_users' FROM users_photos group by u_id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - count_users" . $row["count_users"]. "<br>";
}
} else {
echo "0 results";
}
You can do something like this:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$sql = 'SELECT u_id, count(*) AS count_users FROM users_photos GROUP BY u_id';
$result = mysqli_query($con, $sql);
while ($row=mysqli_fetch_assoc($result)) {
echo 'User id: ' . $row['u_id'] . ' Count: ' . $row['count_users'] . '<br>';
}
Keep in mind this is just a basic example. In a real world application there is more to do such as checking for errors.
I am trying to get 5 bits of data, 5 of them come from tbl_playerstats
these columns are
StatsID
Score
Kills
Deaths
Rank
What im struggling with it that StatsID is linked to another table called tbl_playerdata
In this table the PlayerID is the same as StatsID their values are numeric, also in this tbl_playerdata is SoldierName which is what im am trying to put in place of StatsID , end goal being that only these show:
SoldierName
Score
Kills
Deaths
Rank
My code so far looks like this ( minus database connection details
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StatsID, Score, Kills, Deaths, Rounds FROM tbl_playerstats Limit 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> Soldier: ". $row["StatsID"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rounds"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
I have googled and it looks like using a JOIN is the way to go, the examples that I have found haven't worked for me, this could be down to not really knowing what this is called I'm trying to do.
Join both tables using StatsID and PlayerID
<?php
$sql = "SELECT tps.*, tpd.* FROM tbl_playerstats tps, tbl_playerdata tpd WHERE tps.StatsID = tpd.PlayerID Limit 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<br> Soldier: ". $row["SoldierName"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rounds"] . "<br>";
}
} else {
echo "0 results";
}?>
Hi as your questions is not much clear and also the output is also not much cleared, what you want?
anyway, you can join two tables to get the common columns from those tables like below.
suppose table-1 is player have 5 columns
stats
score
kill
death
rank
and another tabel 2 is playerData having these columns
playerId
playerName
playerEmail
and so on...!!
and you want whole data of table 1 along with the playerEmail, and statId and Playerid is common. then use this query
select p.stats, p.score, p.kill, p.death, p.rank, d.playerEmail
from Player p
inner join playerData d
on p.statid = d.playerid.
try this if your tbl_playerstats main table then use left join with condition tps.StatsID = tpd.StatsID , hope it will help you
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT tpd.SoldierName,tps.Score,tps.Kills,tps.Deaths,tps.Rank FROM tbl_playerstats tps LEFT JOIN tbl_playerdata tpd on tps.StatsID = tpd.StatsID Limit 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> SoldierName: ". $row["SoldierName"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rank"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
and if you want to same data then use innner join
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT tpd.SoldierName,tps.Score,tps.Kills,tps.Deaths,tps.Rank FROM tbl_playerstats tps, tbl_playerdata tpd WHERE tps.StatsID = tpd.PlayerID Limit 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> SoldierName: ". $row["SoldierName"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rank"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
if you face any problem then inform me. i will try to help you
Here's my script so far..
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Category, Genre FROM skills";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> Category: ". $row["Category"]. " - Genre: ". $row["Genre"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
.
I am trying to get a display to read like:
Category: Genre, Genre, Genre. Category: Genre, Genre, Genre.
I have 15 different Categories in mySQL table, with anything between 5-15 genres for each category.
I have done something similar a while ago in Microsoft Access with the help of primary keys, but I am lost without access (now on Mac having to learn php & sql)
First use GROUP_CONCAT() function to concatenate all genres for each category, and then simply loop through the result set.
Here are the relevant references:
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
So you need to change your SQL query from
$sql = "SELECT Category, Genre FROM skills";
to
$sql = "SELECT Category, GROUP_CONCAT(Genre SEPARATOR ', ') as Genre FROM skills GROUP BY Category";
Here's the complete code:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Category, GROUP_CONCAT(Genre SEPARATOR ', ') as Genre FROM skills GROUP BY Category";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
echo "Category: ". $row["Category"]. " - Genre: ". $row["Genre"]. "<br />";
}
}else {
echo "0 results";
}
$conn->close();
I'm trying to build my 1st relationship database and it looks like this so far:
Relationships Table
Partners Table
Location Table
I want to echo all the row info including the related location names on my page, how would I get the following to echo?
ID: 2 Name: Salisbury Removals Locations: Salisbury
ID: 4 Name: Inbetween Removals Locations: Salisbury, Southampton
ID: 5 Name: Southampton Removals Locations: Southampton
=====SOLVED!=====
$sql = "SELECT partner_id, partner_name, email_address, active FROM partners WHERE active ='yes' ORDER BY partner_id ASC";
$connect->query($sql);
if ($partners = $connect->query($sql)) {
foreach ($partners as $partner) {
echo '<li><ul>';
echo '<li>' . $partner['partner_id'] . '</li>';
echo '<li>' . $partner['partner_name'] . '</li>';
echo '<li>' . $partner['email_address'] . '</li>';
echo '<li>' . $partner['active'] . '</li>';
// START GET LOCATIONS FROM RELATED TABLE
echo '<ul>';
$sql2 = "SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from partners_locations r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id =" . $partner['partner_id'] . "";
$connect->query($sql2);
if ($locations = $connect->query($sql2)) {
foreach ($locations as $location) {
echo '<li>' . $location['Locations'] . '</li>';
}
} else {
echo "Error: No Locations<br>";
}
echo '</ul>';
// END GET LOCATIONS FROM RELATED TABLE
echo '</ul></li>';
}
} else {
echo "Error: No Active Partners<br>";
}
The best thing you could do is make a new table to contain the partner_id and location_id.
tbl_relationships_new
Pros for this approach :-
1).When you need to remove a location from a partner, you wouldn't need to edit the column locations. You could simply delete an entry from this new table.
2). When you need to add more data in the locations field, you could simply just insert into the new table, which is rather easy than having to update partners.locations.
Now, you could use easy left joins to get the required data.
SQL query for my table solution.
SELECT t.*,p.*,l.* FROM tbl_relationships_new t, partners p, locations l LEFT JOIN
partners
ON
t.partner_id = p.partner_id
LEFT JOIN
locations
ON
l.location_id = t.location_id
WHERE
t.partner_id = 2
UPDATE
Here are the queries based on your table structure.
1). ID: 2 Name: Salisbury Removals Locations: Salisbury
SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id = 2
2). When there are 2 locations.
SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id = 5
UPDATE
Solution without explicitly mentioning an ID.
SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id
<?php
$dbconn = mysqli_connect(DBSERVER, DBUSER, DBPWD, DBNAME);
$sql = "SELECT partner_id, partner_name, locations FROM Partners";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: ".$row['partner_id']." Name: ".$row['partner_name']." Locations: ";
$locs = explode(",",$row['locations']);
$first = true;
foreach ($locs as $loc) {
$sql2 = "SELECT location_name FROM Locations WHERE location_id = '".$loc."'";
$result2 = mysqli_query($dbconn, $sql2);
$row2 = mysqli_fetch_assoc($result2);
$separator = ($first?"":", ");
$first = false;
echo $separator.$row2['location_name'];
}
echo "<br/>";
}
?>
First your table Table: Partners entry should be like this.
partener_id | partner_name | email_address | active | location_id
2 Test Partner ....... yes 1
5 Good Removals ....... yes 1
5 Good Removals ....... yes 2
4 Special Removals ....... yes 2
So, you could make join with Table: Locations.
After that execute below sql query.
$sql = "select * from partners Left join locations on locations.location_id=partners.location_id";
For better understanding of query execution and data display. follow below link.
http://www.w3schools.com/php/php_mysql_select.asp
Your "locations" field in the Partners table is wrong, you must create another table to handle relations between Partners and Locations, called Locations_Partners par example. This table must have 2 fields, location_id and partner_id. This way, you can relate many partners with many locations. Then, you code should look like this:
<?php
$servername = "www.server.com";
$username = "username";
$password = "password";
$dbname = "your database name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT partner_id, partner_name, location_name FROM Partners
INNER JOIN Locations_Partners ON Partners.partner_id = Locations_Partners.Partner_id
INNER JOIN Locations ON Locations_Partners.Location_id = Locations.location_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["partner_name"]. " " . $row["location_name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I am looking to filter results by date, which I gotten to work just fine. Now I'm trying to have the database return values based on each employee in that filtered period. Example in the employee table:
ID Hourly
then in the data table
ID Hours
So I was trying for if the IDs match then look up how much the hourly and hours would total out to. Here's what I have thus far.
<?php
$con=mysqli_connect("MY_INFO.com","USER","*********","DB_NAME");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$data = mysqli_query($con,"Select * FROM employees
Select * FROM data
SELECT t1 *, t2.*
From employees t1
INNER JOIN data t2 on t1.id = t2.id
'");
while($data = mysqli_fetch_array($data)) {
echo $data['id'] . " " . $data['id'];
echo "<br>";
}
$result = mysqli_query($con,"SELECT * FROM friends
WHERE date >'2014-09-1' and date < '2014-9-30'");
while($row = mysqli_fetch_array($result)) {
echo $row['name'] . " " . $row['id'];
echo "<br>";
I'm trying to filter by date, and then match IDs from both tables so if an id equals an id from the other table then it could return an hourly wage. So if employee id: 1 had an hourly wage of $20 / hr and worked 2 hours then I could get it to return $40. The tables would have in them:
Employee:
ID $/hr
1 20
Data:
ID Hours
1 2