I have two table where I want to query with join two table.
If there is pro_pic = 'NULL' than the result I want to get without null pro_pic.
Here is the Table:
user_info
ID user_id full_name country
--------------------------------------
1 Star01 Sagor Us
2 Star02 Rofiq India
3 Star03 Karim Aus
pro_pic
ID user_id pro_pic
---------------------------------
1 Star01 14523.png
2 Star02 NULL
3 Star03 554513.png
so I want to result like
ID user_id full_name country pro_pic
------------------------------------------------------------------
1 Star01 Sagor Us 14523.png
3 Star03 Karim Aus 554513.png
the null pro_pic field not showing.
Here is my query but I didn't got the result :'(
<?php
$stmt1 = mysqli_query($con,"SELECT ui.user_id, pp.pro_pic, pp.user_id,
ui.full_name, ui.country
FROM user_info
INNER JOIN pro_pic ON ui.user_id=pp.user_id where ui.show_hide_profile != '1' and pp.pro_pic != '' limit $current, $limit");
while($user_info = mysqli_fetch_array($stmt1)){
echo $user_id_view = $user_info['user_id'];
echo $full_name = $user_info['full_name'];
echo $country = $user_info['country'];
echo $pro_pic = $user_info['pro_pic'];
}
?>
Please help me thanks to all
Try this
$stmt1 = mysqli_query($con,"SELECT ui.user_id, pp.pro_pic, pp.user_id, ui.full_name, ui.country
FROM user_info ui
INNER JOIN pro_pic pp ON ui.user_id=pp.user_id where pp.pro_pic IS NOT NULL
ORDER BY ui.user_id limit $current, $limit");
EDIT: Added order by clause
Please note whenever you use limit clause you should use order by clause because if you are not using order by clause the MySQL result always return random rows
Use this query
$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
Here are the link to the code:
Join two mysql tables with php
Hope it helps.
Related
I am having 2 tables :
1.internal_employee_master
id employee_name unique_id
1 Noah ABCD
2 Liam ABCD
3 William ABCD
4 Benjamin ABCD
5 Jacob EFGH
2.external_employee_master
id name unique_id
1 Elijah ABCD
2 Ethan ABCD
3 Alexander EFGH
I am using UNION query to get both tables data into single table and display this data into html table.
select id
, employee_name
, unique_id
from internal_employee_master
where unique_id = 'ABCD'
union
select id
, employee_name
, unique_id
from external_employee_master
where unique_id = 'ABCD'
I want to store payslips of both employees into single table.
I have one table payslips with emp_id and emp_type columns.
I am storing data into payslips data like:
id pay_slip emp_id emp_type
1 Noah_payslip.pdf 1 internal
2 Liam_payslip.pdf 2 internal
3 Lia_payslip.pdf 1 External
as you can see in above table i am storing emp_id and emp_type of
both the tables in single columns each.
Now, i dont undestand how to split data of internal employee and
external employee from pay_slip table and show data in html table.
Currently, i am writing below sql joins to get employee_names of
internal and external employee tables but it doesnt work for me.
$id = $_GET['id];
SELECT ps.id,ps.pdf,ps.emp_id,ps.emp_type,external_employee.name as comemp,
internal_employee.comp_empl_name as comemp
FROM pay_slip as ps
INNER JOIN internal_employee_master as internal_employee ON internal_employee.comp_trad_id = ps.trade_id
INNER JOIN external_employee_master as external_employee ON external_employee.trad_id = ps.trade_id
where ps.is_deleted = 1 AND ps.id = '".$id."'"
Please help me to join query to get name and employee_name with respect to emp_type form pay_slip table.
How about using UNION again?
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
internal_employee_master AS internal_employee ON internal_employee.comp_trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'internal'
UNION ALL
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
external_employee_master AS external_employee ON external_employee.trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'external'
You could try this
SELECT ps.id, ps.pay_slip, ps.emp_type, COALESCE(i.employee_name, e.name) AS name
FROM payslips ps
LEFT JOIN internal_employee_master i ON i.id = ps.emp_id AND ps.emp_type = 'internal'
LEFT JOIN external_employee_master e ON e.id = ps.emp_id AND ps.emp_type = 'External'
AND ps.id = :ID
You can see this in action here http://sqlfiddle.com/#!9/53a195/7/0
I would mention that there are a number of issues in your included tables and queries. For example, irregular column names between tables (name vs. employee_name), you've missed the is_deleted column from your example schema, and you have capitalised and non-capitalised values in the emp_type column which is confusing.
I need one help .I need to fetch some data after joining the multiple table using PHP and Mysql so i need the appropriate query for that.I am explaining my table structure below.
db_order:
id order_id promocode
1 10 A12016
2 11 A12016
db_order_product:
id order_id pro_data_id quantity
1 10 20 2
2 10 22 3
3 11 20 1
db_product_info:
pro_data_id product_name
20 abc
22 xyz
I have tried something like below but its not working.
$sqlqry="select * from db_order order by id desc";
$orderqry=mysqli_query($con,$sqlqry);
while($row=mysqli_fetch_assoc($orderqry)){
$order_id=$row['order_id'];
$sqlproqry="select * from db_order_products where order_id='".$order_id."'";
$proqry=mysqli_query($con,$sqlproqry);
while($row1=mysqli_fetch_assoc($proqry)){
$product_data_id=$row1['pro_data_id'];
$sqldataqry="select * from db_product_data where pro_data_id='".$product_data_id."'";
$prodataqry=mysqli_query($con,$sqldataqry);
while($prodatarow=mysqli_fetch_assoc($prodataqry)){
$pro_id=$prodatarow['pro_Id'];
$sqlpro="select * from db_product_info where pro_Id='".$pro_id."'";
$prodata=mysqli_query($con,$sqlpro);
$prorow=mysqli_fetch_array($prodata);
}
}
$result[]=array('id'=>$row['id'],'order_id'=>$row['order_id'],'promocode'=>$row['promocode'],'order_pro_id'=>$row1['id'],'pro_data_id'=>$row1['pro_data_id'],'pro_quantity'=>$row1['quantity'],'product_name'=>$prorow['Product_name']);
}
echo json_encode($result);
Here i need first user will go to db_order table fetch all value and according to the order_id the all data should fetch from db_order_product then as per pro_data_id from db_order_product table it will fetch data from db_product_info table and finaly return all data in an array.Please help me.
Just use a simple JOIN
SELECT o.id, o.order_id, o.promocode, p.id AS order_pro_id, p.prod_data_id, p.quantity AS pro_quantity, i.product_name
FROM db_order AS o
JOIN db_order_products AS p ON o.order_id = p.order_id
JOIN db_product_data AS d ON p.pro_data_id = d.pro_data_id
JOIN db_product_info AS i ON i.pro_Id = d.pro_Id
ORDER BY o.id DESC
you must fix you query:
use this query instead:
select * from db_order,db_order_product,db_product_info where db_order.order_id=db_order_product.order_id and db_order_product.pro_data_id=db_product_info.pro_data_id
SELECT dor.id,dordor.order_id,dor.promocode
FROM db_order AS dor
JOIN db_order_product AS dop ON dop.order_id = dor.order_id
JOIN db_product_info as dpi ON dpi.pro_data_id=dop.pro_data_id
I have a table which stores clients like this:
id name
-- ----
1 John
2 Jane
...
I also have another table which stores links created by clients:
id client_id link created
-- --------- ---- -----------
1 1 ... 2015-02-01
2 1 ... 2015-02-26
3 1 ... 2015-03-01
4 2 ... 2015-03-01
5 2 ... 2015-03-02
6 2 ... 2015-03-02
I need to find how many links a client has created today, this month and during all the time. I also need their name in the result, so I'll be able to craete a HTML table to display the statistics. I thought I can code as less as possible like this:
$today = $this->db->query("SELECT COUNT(*) as today, c.id as client_id, c.name FROM `links` l JOIN `clients` c ON l.client_id = c.id WHERE DATE(l.created) = CURDATE() GROUP BY c.id");
$this_month = $this->db->query("SELECT COUNT(*) as this_month, c.id as client_id, c.name FROM `links` l JOIN `clients` c ON l.client_id = c.id WHERE YEAR(l.created) = YEAR(NOW()) AND MONTH(l.created) = MONTH(NOW()) GROUP BY c.id");
$yet = $this->db->query("SELECT COUNT(*) as yet, c.id as client_id, c.name FROM `links` l JOIN `clients` c ON l.client_id = c.id WHERE GROUP BY c.id");
And then merge them in PHP as I asked HERE before, like this:
$result = array_replace_recursive($today, $this_month, $yet);
So I'll be able to loop into the result and print my HTML table.
But there are logical problems here. Everything works fine, but the result in a month is a wrong number, forexample the whole created links of one person is 1 but it shows 4 in the monthly counter! I also tried to use RIGHT JOIN in SQL query to get all clients, so array_replace_recursive in PHP could work fine as I think it doesn't work properly at the moment, but no success and got wrong results again.
Can anyone show me a way to make the job done?
This query should do it for today
$query_today="
SELECT name, id AS user_id, (
SELECT COUNT( * )
FROM links
WHERE client_id = user_id AND created = '2015-03-02'
) AS alllinks
FROM clients"
adjust the WHERE clause in the subquery for months and all
$query_month="
SELECT name, id AS user_id, (
SELECT COUNT( * )
FROM links
WHERE client_id = user_id AND created like '2015-03%'
) AS alllinks
FROM clients"
$query_all="
SELECT name, id AS user_id, (
SELECT COUNT( * )
FROM links
WHERE client_id = user_id
) AS alllinks
FROM clients"
I have a table that looks like this:
id | user_id | credits
----------------------
1 | 2 | 300
2 | 2 | 200
3 | 4 | 100
I need a select or way to add the credits from a specific user id
The select would be:
SELECT credit FROM myTable WHERE user_id ='2'
The result I would need in this case would be 500
How can I do this?
Use SUM() in your query will solve the issue.
SELECT SUM(`credits`) AS `total` FROM `myTable` WHERE `user_id` =2
Since you specify PHP tag, the below code will help you.
$mysqli=mysqli_connect("hostname","username","password","databasename");
$query = "SELECT SUM(`credits`) AS `total` FROM `myTable` WHERE `user_id` =2";
$processquery = $mysqli->query($query);
$result = $processquery->fetch_assoc();
echo $result['total'];
Use sum()
SELECT sum(credit) as sum_credit
FROM myTable
WHERE user_id = 2
Use aggregate function SUM():
SELECT SUM(credit) as TotalCredit
FROM myTable
WHERE user_id='2'
Read more about aggregate functions here.
Try SUM() in mysql
SELECT SUM(credit) as totalcredit FROM myTable WHERE user_id ='2'
will return you matched result sum according to condition
select sum(credit) as total from myTable where user_id = '2'
You can do this by summing the total of the credit column. Check out the MySQL reference manual for the official instructions.
SELECT SUM(credit) AS credit_total
FROM myTable
WHERE user_id = 2
You use the AS keyword to assign a name to your column, then in PHP you can access this column as you would any other by name, just using credit_total. E.g:
$query = "SELECT SUM(credit) AS credit_total
FROM myTable
WHERE user_id = 2";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row['credit_total']; // Will output 500
i have made a query to find how many people have voted for an entry and it goes like this:
$query = "SELECT itemid, COUNT(*) totalcount
FROM jos_sobi2_plugin_reviews
GROUP BY itemid
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
";
The sql works and produces the following table.
entry id totalcount
-------- ----------
202 5
203 20
204 15
...
I only want to display the column totalcount and i dont succeed.
maybe someone knows which query should i right on my php?
thanks, ronen!
$query = "SELECT COUNT(1) totalcount
FROM jos_sobi2_plugin_reviews
GROUP BY itemid
HAVING COUNT(1) > 1
ORDER BY COUNT(1) DESC
";
This could work fine