I have a MySQL database that looks similar to this:
COUPON TABLE
| id | coupon_id | host | away |guess |skor |status|
+----+-----------+-----------+-------------+---------+------+------+
| 1 | 22 | Barcelona | Real Madrid | 1 |2-1 | 1 |
| 2 | 22 | Celtic | Porto | 2 |1-0 | 2 |
| 3 | 23 | Barcelona | Real Madrid | 1 |2-1 | 1 |
| 4 | 23 | M city | Chelsea | 1 |4-1 | 1 |
COUPONS TABLE
| id | user | Copuonstatus
+----+------------+-------------+
| 22 | Admin | 0 |
| 23 | Roberto | 0 |
I need to sort the results in a table that looks like this:
COUPONS TABLE
| id | user | Copuonstatus
+----+------------+-------------+
| 22 | Admin | 2 |
| 23 | Roberto | 1 |
NOTE :
Copuonstatus= default value 0
status = 0 waiting
status = 1 win
status = 2 los
im query try,
I could not
or update INNER JOIN use?
$q=$db->prepare("SELECT * FROM coupons INNER JOIN coupon ON
coupon.coupon_id = coupons.id AND coupon.status=2");
$q->execute();
$sss=$q->fetchAll(PDO::FETCH_GROUP);
if ( $sss > 0){
$db->exec("UPDATE coupons SET coupon_status = 2 FROM coupons ON
coupon.coupon_id = coupons.id WHERE coupons.Copuonstatus=0 ");}
UPDATE
coupons AS A
INNER JOIN coupon AS B ON A.id = B.coupon_id
SET
coupon_status = 2
WHERE
A.Copuonstatus = 0
Hope it help
Related
i have a series of tables for an asset checkout system. there are 2 types of assets.. Stocks and Assets. they all have barcodess stored in a separate table. this is how my tables look
Checkouts table: (sipe_check_out)
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
| id | job_id | stock_id | asset_id | qty | out_time | user_out | note | depot_id |
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
| 1 | 1625 | 0 | 1026 | 1 | 0000-00-00 00:00:00 | 1288 | | 1 |
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
| 2 | 1625 | 8 | 0 | 10 | 0000-00-00 00:00:00 | 1288 | | 0 |
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
barcodes table: (sipe_barcodes)
+----+----------+----------+---------+
| id | stock_id | asset_id | barcode |
+----+----------+----------+---------+
| 1 | 0 | 1026 | AR2221 |
+----+----------+----------+---------+
| 2 | 8 | 0 | MAR0001 |
+----+----------+----------+---------+
Stock Table: (sipe_stock)
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
| id | title | alt_title | status | category | last_updated | memo | replace_cost | flag | description |
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
| 8 | Cable XLR | Cable XLR | 2 | 10 | 2019-01-03 20:45:40 | | 750 | 0 | |
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
| 237 | Fresnel 1K | | 0 | 43 | 2019-01-02 19:08:45 | | 12000 | 0 | |
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
Asset Table: (sipe_stock_asset)
+------+----------+-----+--------+--------+---------------------+------+---------------+-------------+-------+
| id | stock_id | qty | serial | status | status_change | memo | purchase_date | retire_date | depot |
+------+----------+-----+--------+--------+---------------------+------+---------------+-------------+-------+
| 1026 | 237 | 1 | | 0 | 2019-01-02 19:09:00 | NULL | NULL | NULL | 1 |
+------+----------+-----+--------+--------+---------------------+------+---------------+-------------+-------+
i need a list of all items checked out to a specific job_id. these items are sotred in the chekout table. but for display purpose i need to get the items barcode (stored in the barcodes table) , items title (stored in the stock table), and qty (stored in the checkout table)
this is my problem. the select query is different for a stock item and for an asset item.
if it is a stock item one needs to get the stock id from the checkout table then get the title from the stock table and the qty from the checkout table.
this is my working query to get stock items
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM
sipe_barcodes
INNER JOIN sipe_stock ON (sipe_barcodes.stock_id = sipe_stock.id)
INNER JOIN sipe_check_out ON (sipe_stock.id = sipe_check_out.stock_id)
WHERE
sipe_check_out.job_id = 1625
this returns the following
+---------+-----------+-----+---------------------+
| barcode | title | qty | out_time |
+---------+-----------+-----+---------------------+
| MAR0001 | Cable XLR | 10 | 0000-00-00 00:00:00 |
+---------+-----------+-----+---------------------+
If the item is a a stock. we get the asset_id from the checkout. but then we need to get the stock_id from the checkout table so we can get the stock title from the stock table. the qty from the checkout table and the barcode from the barcode table
this is my working query to get asset items
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM sipe_check_out
INNER JOIN sipe_stock_asset ON (sipe_check_out.asset_id = sipe_stock_asset.id)
INNER JOIN sipe_stock ON (sipe_stock_asset.stock_id = sipe_stock.id)
LEFT JOIN sipe_barcodes ON (sipe_check_out.asset_id = sipe_barcodes.asset_id)
WHERE sipe_check_out.job_id = 1625
this retuns this
+---------+------------+-----+---------------------+
| barcode | title | qty | out_time |
+---------+------------+-----+---------------------+
| AR2221 | Fresnel 1K | 1 | 0000-00-00 00:00:00 |
+---------+------------+-----+---------------------+
The Problem
how do i structure a single mysql query that can select all items checked out no matter if they are assets or stocks producing the following output
+---------+------------+-----+---------------------+
| barcode | title | qty | out_time |
+---------+------------+-----+---------------------+
| AR2221 | Fresnel 1K | 1 | 0000-00-00 00:00:00 |
+---------+------------+-----+---------------------+
| MAR0001 | Cable XLR | 10 | 0000-00-00 00:00:00 |
+---------+------------+-----+---------------------+
your help is much Appreciated
Thanks
You may use UNION with your provided select statements
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM
sipe_barcodes
INNER JOIN
sipe_stock ON (sipe_barcodes.stock_id = sipe_stock.id)
INNER JOIN
sipe_check_out ON (sipe_stock.id = sipe_check_out.stock_id)
WHERE
sipe_check_out.job_id = 1625
UNION ALL
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM
sipe_check_out
INNER JOIN
sipe_stock_asset ON (sipe_check_out.asset_id = sipe_stock_asset.id)
INNER JOIN
sipe_stock ON (sipe_stock_asset.stock_id = sipe_stock.id)
LEFT JOIN
sipe_barcodes ON (sipe_check_out.asset_id = sipe_barcodes.asset_id)
WHERE
sipe_check_out.job_id = 1625
I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;
there is 4 table
team
---------------
id | teamname |
1 | lal |
2 | sobuj |
member
-----------------------
id | membername| team |
1 | sagor | 1 |
2 | sumon | 1 |
dps
---------------------------------------------------
id | member| team | deposit | refund | ddate |
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
mvs
----------------------------------------------------
id | member| team | mvs_total| refund | ddate |
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
here is my query:
$result=mysql_query(select * from member where team='1');
$row=mysql_fetch_array($result);
do{
$aresult=mysql_query(select sum(deposit)-sum(refund)as balance from dps where member='$row[id]');
$arow=mysql_fetch_array($aresult);
echo $arow[balance];
$bresult=mysql_query(select sum(mvs_total)as mvs_balance from mvs where member='$row[id]' and date1<='2016-09-01');
$brow=mysql_fetch_array($bresult);
echo $brow[mvs_total];
} while($row=mysql_fetch_array($result));
its taking too much time how to avoid sub query or how can i save time advice plz Thanks in advance.
First thing I notice in your table structure is that repetition of column.
E.g. team column not require in dps and mcs table, because from member column [connect to member table] you can easily find dps or mvs is under which team.
For your current problem you can use following query.
SELECT member.id,
member.membername,
Sum(dps.deposit) - Sum(dps.refund) AS balance,
Sum(mvs.mvs_total) AS mvs_balance,
Sum(loan.deposit) - Sum(loan.withdraw) AS balance,
Sum(loan.deposit2) - Sum(loan.withdraw2) AS balance2,
Sum(loan.total) - Sum(loan.lpay) AS outsstanding
FROM member AS member
LEFT JOIN dps AS dps
ON dps.member = member.id
LEFT JOIN mvs AS mvs
ON mvs.member = member.id
LEFT JOIN loan AS loan
ON loan.member = member.id
WHERE member.team = 1
AND mvs.ddate <= '2016-09-01'
-- AND loan.team = '10'
-- AND loan.cs = 'Core'
GROUP BY member.id
I get form db field (organisations.paths)the following strings:
/1/2/3/4
Each number is the id of organisation's name from this db table:
+----+-------------+----------+----------+----------------------+
| id | frameworkid | path | parentid | fullname |
+----+-------------+----------+----------+----------------------+
| 1 | 1 | /1 | 0 | NYC University |
| 2 | 1 | /2 | 0 | Board of directors |
| 3 | 1 | /1/2/3 | 1 | Math faculty |
| 4 | 1 | /1/2/3/4 | 3 | Statistic department |
| 5 | 1 | /1/2/3/5 | 2 | Linguist department |
+----+-------------+----------+----------+----------------------+
Then I have the description table for each organisation:
+----+----------+---------+----------------+
| id | data | fieldid | organisationid |
+----+----------+---------+----------------+
| 1 | HQ | 1 | 1 |
| 2 | advisory | 1 | 2 |
| 3 | advisory | 1 | 3 |
| 4 | bottom | 1 | 4 |
| 5 | advisory | 1 | 5 |
+----+----------+---------+----------------+
How to join the both description table and main table and loop only through organisations, which have HQ or advisory in their description? So it becomes:
NYC University, Board of directors, Math faculty (Statistic department-won't be shown, as it is with description bottom)
You need to use explode, IN and join Function of PHP and mysql.
$var = "/1/2/3/4";
$in = join(" , ", explode("/", ltrim($var, '/')));
$sql = "SELECT `dt`.`fullname` FROM `db_table` dt
LEFT JOIN `organization` o
ON `o`.`organisationid` = `dt`.`id`
WHERE `o`.`id` IN ($in) AND (`o`.`data` = 'HQ' OR `o`.`data` = 'advisory')";
Make a loop to get the names and show them as you want.
I have 2 MySQL tables, one of which has a numeric column to orgainise the order I need the items to be displayed:
item_names
menu_id | dish_id | section_id | item_name
--------------------------------------------------
1 | 23 | 2 | Pie
1 | 24 | 2 | Fish
1 | 25 | 3 | Apples
1 | 26 | 2 | Onions
1 | 27 | 2 | Chips
link_extras
extra_id | dish_id | sort
-----------------------------
1 | 23 | 2
2 | 23 | 2
3 | 23 | 2
1 | 24 | 0
5 | 24 | 0
6 | 26 | 3
12 | 26 | 3
1 | 27 | 1
1 | 25 | 0
Basically what I am trying to do is extract each dish with a certain menu_id and section_id from the table item_names and order the output in respect to the sort column in the link_extras table.
so far:
$query="SELECT a.item_name, a.dish_id, b.sort
FROM item_names AS a, link_extras AS b
WHERE a.menu_id='1'
AND a.section_id='2'
AND b.dish_id=a.dish_id
GROUP BY b.dish_id
ORDER BY b.sort";
I am quite new to databases so would appreciate any help. The result I am after is
Fish
Chips
Pie
Onions
Unfortunately just can't get the order correct.
You need to use a simple JOIN
SELECT a.item_name, a.dish_id, b.sort
FROM item_names AS a
JOIN link_extras AS b
ON a.dish_id = b.dish_id
WHERE menu_id = 1
AND section_id = 2
GROUP BY b.dish_id
ORDER BY b.sort
Output:
| ITEM_NAME | DISH_ID | SORT |
------------------------------
| Fish | 24 | 0 |
| Chips | 27 | 1 |
| Pie | 23 | 2 |
| Onions | 26 | 3 |
See this SQLFiddle
SELECT
in.item_name
FROM item_names AS in
LEFT JOIN link_extras AS le
ON le.dish_id = in.dish_id
WHERE in.menu_id = 1
AND in.section_id = 2
ORDER BY le.sort
Demo Here
Output
| ITEM_NAME |
-------------
| Fish |
| Chips |
| Pie |
| Onions |