Select statement to display the result with combination of 2 columns - php

Hi I am new for developing.Kindly bear my codings. I have created a table arlog with id(auto increment), status, ticket number and code. Ticket and code number is set as unique. That is the duplicate of this combination cannot inserted again. But individually ticket number or cpt can be inserted as many times.It works fine. Now I want to use select query with another table with respect to the arlog table ticket number and code.Here is the select statement
$result = mysql_query("SELECT * FROM `ar` C WHERE provider='".$_SESSION['PROVIDER']
."' AND C.`TicketNo` IN ( SELECT TicketNo FROM `arlog` L where L.status NOT IN('New','Completed','Completed(Ar_aging)
','Completed(Rework)','Rework','Completed_Followup','Completed_Supervising' )
and L.assign='".$_SESSION['NAME']."' ) order by id desc") or die(mysql_error());
The query check the ticket number in arlog and displays correcly. But I want to combine TicketNo and Code in the arlog. I have made research but could not find solution. First of all is it possible?

Please try following sql:
SELECT L.TicketNo ,L.Code,C.* FROM `ar` C left join `arlog` L ON C.TicketNo = L.TicketNo where C.provider='your condition' and L.status NOT IN('New','Completed','Completed(Ar_aging)','Completed(Rework)','Rework','Completed_Followup','Completed_Supervising' ) and L.assign='your condition' order by by C.id desc
Hope this can help you!

I think you need to use CONCAT_WS()
There is a nice example of its usage in below link
MySQL SELECT AS combine two columns into one

Related

SUM 2 values in mysql to make leaderboard

I want make a leaderboard from my database, I already make a code but the sql says error
"SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun)
MySQL said: Documentation
#1242 - Subquery returns more than 1 row"
heres my sql query :
SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun);
SET #a2 = (SELECT SUM(j.nilai_jawaban)
FROM jawaban j
GROUP BY j.id_akun);
SET #hasil = #a1 + #a2;
SELECT #hasil
Thank you !
this is an example data in table jawaban:
this is an example data in table diskusi:
and my desired answer:
where tera123 have 135 TOTAL(d.poin_diskusi+j.nilai_jawaban) and david123 have 90 TOTAL(d.poin_diskusi+j.nilai_jawaban)
the point is to show TOTAL for every user
It looks to me (without looking at your images; if you want to show information, please provide it in text) like you want something like:
select id_akun, sum(point_value) as total_points from (
select id_akun, sum(poin_diskusi) as point_value
from diskusi group by id_akun
union all
select id_akun, sum(nilai_jawaban) as point_value
from jawaban group by id_akun
) as point_values
group by id_akun
The problem are the GROUP BY keywords. Inside the images you provided with example data, one can see that there are multiple entries for the columns jawaban.id_akun and diskusi.id_akun. Because of that, both SELECT will return multiple rows as result, for each different value of the id columns one summed up row. Try it without the GROUP BY clause.

mysql - group results by id and print

I have "reservation" table (mySql) that contain number of columns: res_id, hotel_id, hotel_name, from_date, to_date.
I would like to select and print html table for each hotel (i'm using PHP). the result should be a title - the name of the hotel, and bellow it a list of reservation for the specific hotel.
I can do GROUP BY:
Select * FROM reservation GROUP BY hotel_id
I'm not sure if it's the right way to do it, and how do i print the results without checking all the time if the hotel_id was changed?
Thank you in advanced
GROUP BY is definitely NOT the right way to approach this. One method would be:
SELECT *
FROM reservation
ORDER BY hotel_id;
You would then loop through the result sets. When the hotel name changes, you would put in the title of the hotel.
Note: This is a poor data model if it has both the hotel id and name in reservation. This would normally be in hotel and you would connect the tables using JOIN:
SELECT h.hotel_name, r.*
FROM hotels h JOIN
reservation r
ON r.hotel_id = h.hotel_id
ORDER BY hotel_id;
Using a LEFT JOIN, you can even get hotels with no reservations.
How is it that the hotel_id would change? As per your question it seems that hotel_id is a column made for join with a "hotels" table, isn't it?
Regarding the "group by", why would you group by hotel? This would make you loose reservations data, unless you were using some sort of group_concat.
If you want to get the reservations from a specific hotel you could loop through your hotels table and inside your loop you can do:
SELECT * FROM reservations WHERE hotel_id='QUERIED_HOTEL_ID'
Then show the results.
Or you could simply
SELECT * FROM reservations
And when you get the fetched results you can make a multidimensional php array with 'hotel_id' as top level key and 'res_id' as secondary, like this:
$reservations_by_hotel = [];
do {
$resId = $row['res_id'];
$hotelId = $row['hotel_id'];
$reservations_by_hotel[$hotelId][$resId] = $row;
} while ($row = $result->fetch_assoc());

PHP Query within a while fetch loop

I was wondering if it was possible to run a query inside a while loop which is used to display the content of a SQL table.
Here is the code if I'm not clear enough :
$sql="SELECT * FROM hotels WHERE rooms>0";
$req=$db->query($sql);
while($row=$req->fetch()){
//The second query to check how many place is left
$req2=$db->query('SELECT COUNT(*) FROM people WHERE idhotels='.$row["idhotels"].';');
echo "hey".$req2;
$left_rooms= $row["rooms"] -$req2;
echo '<option value="'.$row["idhotels"].'">'.$row["name_hotel"].' ('.$left_rooms.' rooms left)</option>';
}
What I'm trying to do here, is to display a list of hotels with the number of rooms left. The problem is I have to count how many rooms are taken before displaying the number of rooms left, hence the second request.
My code obviously doesn't work, but I can't figure out why.
Can someone help me ?
Many thanks !
Why not using a join and a group by so you only have one query ?
$sql="SELECT h.idhotels,h.name_hotel,count(*) FROM hotels h inner join people p on h.idhotels = p.idhotels WHERE h.rooms>0 group by h.idhotels,h.name";
while($row=$req->fetch()){
// Here do whatever you want with each row
}
Have you tried to calculate your left rooms in the database with a joined query like:
SELECT rooms - COUNT(*) AS left_rooms FROM hotels h WHERE rooms > 0 JOIN people p ON (p.idhotels = h.idhotels) GROUP BY h.idhotels, h.name ORDER BY left_rooms ASC;

Mysql Join Won't Select

I have been doing a lots of research online and from my understanding i think my query is ok
That is why i need your help to point me out what im doing wrong.
What My Query Should Do
My query should fetch our stock level from both warehouse
Problem Is
if the product is not in both warehouse the query dont give any result.
Ok so first i have two database of warehouse stock level. that look like that.
Databases
-warehouse1
-warehouse2
Table
-product
Columns
-id
-SKU
-qty
So my Query is
SELECT
warehouse1.product.id as 1_id,
warehouse2.product.id as 2_id ,
warehouse1.product.SKU,
warehouse1.product.qty as 1_qty,
warehouse2.product.qty as 2_qty
FROM `warehouse1`.`product`
LEFT JOIN `warehouse2`.`product`
ON
(`warehouse1`.`product`.`SKU` = `warehouse2`.`product`.`SKU`)
WHERE
warehouse1.product.SKU = '$sku'
OR
warehouse2.product.SKU = '$sku'
ORDER BY
(1_qty + 2_qty) DESC
if i make the where clause like this
WHERE warehouse1.product.SKU = '$sku'
it is then working but i can't get stock from both warehouse.
What should i do if i want to receive the stock level from both warehouse even if there is no product that im asking for in this database.
Thanks
Try a FULL OUTER JOIN. You're using a LEFT JOIN. That requires that the DB fetch all records that match your WHERE clause on the LEFT side of the join, which is warehouse1, and any potentially matching records from warehouse2 (the right side of the join). If a SKU exists only in warehouse2, you don't see it.
Switching to a FULL OUTER JOIN forces the DB to fetch all matching records from BOTH sides of the join, regardless of which side(s) the matching records exist on.
you can also do this with a union
(SELECT
warehouse1.product.id as 1_id,
warehouse1.product.SKU,
warehouse1.product.qty as 1_qty
FROM `warehouse1`.`product`
WHERE
warehouse1.product.SKU = '$sku' )
union
(SELECT
warehouse2.product.id as 2_id ,
warehouse2.product.SKU,
warehouse2.product.qty as 2_qty
FROM `warehouse2`.`product`
WHERE warehouse2.product.SKU = '$sku' )
Combine your OR's in () (... OR ...):
SELECT
warehouse1.product.id as 1_id,
warehouse2.product.id as 2_id ,
warehouse1.product.SKU,
warehouse1.product.qty as 1_qty,
warehouse2.product.qty as 2_qty
FROM `warehouse1`.`product`
LEFT JOIN `warehouse2`.`product`
ON
(`warehouse1`.`product`.`SKU` = `warehouse2`.`product`.`SKU`)
WHERE (warehouse1.product.SKU = '$sku'
OR
warehouse2.product.SKU = '$sku')
ORDER BY
(1_qty + 2_qty) DESC

MySQL query that checks in another table

I've got a problem with two mysql tables. I've done some code and I think I am close to the solution, but I'm not sure if this is right.
So here are the two tables:
Table 1: Blogs
Columns: ID, agp_name, agp_url, agp_username, agp_password
Table 2: Keywords
Columns: ID, agp_user_id, agp_order_id, agp_blog_id, agp_keywords, agp_keywords_date
What I want is to get one random row from Table1 based on the following condition: if the agp_keyword match one of the keywords in the last 5 days then do not include into the result.
So far I did this:
SELECT
t1.agp_user_id, t1.agp_order_id, t1.agp_blog_id, t1.agp_keywords, t1.agp_keywords_date, t2.agp_name, t2.agp_url, t2.agp_username, t2.agp_password
FROM table1 AS t1
INNER JOIN (
SELECT ID, agp_name, agp_url, agp_username, agp_password, agp_blogposts
FROM table2
) AS t2 ON t1.agp_blog_id = t2.ID
WHERE
t1.agp_keywords NOT LIKE "%keyword1%" AND
t1.agp_keywords NOT LIKE "%keyword2%" AND
t1.agp_keywords_date BETWEEN (1369440000 AND 1369932432)
ORDER BY RAND() LIMIT 1
However this does not work correctly. Any help will be appreciated.
Try this, your original specifications were a bit confusing :(
SELECT keywords.agp_user_id,
keywords.agp_order_id,
keywords.agp_blog_id,
keywords.agp_keywords,
keywords.agp_keywords_date,
blogs.agp_name,
blogs.agp_url,
blogs.agp_username,
blogs.agp_password
FROM blogs
LEFT JOIN keywords
ON keywords.agp_blog_id = blogs.ID
AND keywords.agp_keywords NOT LIKE "%keyword1%"
AND keywords.agp_keywords NOT LIKE "%keyword2%"
AND FROM_UNIXTIME(keywords.agp_keywords_date) > (DATE_SUB(CURDATE(), INTERVAL 5 DAYS))
ORDER BY RAND() LIMIT 1

Categories