With eloquent to make search by 2 fields - mysql

Is there a way in Laravel 5.6/MySQL 5.7 with eloquent to make search with condition:
need to check if sale_price is set then search in sale_price else in retail_price
Have I use condition like :
\DB::raw( ‘ isNull( sale_price, retail_price ) > 10’ )
and how to modify the search above if both sale_price and retail_price are null?
Or there is better solution ?
MODIFIED :
I implemented it as scope :
public function scopeGetByDoublePrice($query, $price_from= null, $price_to= null)
{
// need to check if sale_price is set then search in sale_price else in retail_price
if ( empty($price_from) and empty($price_to) ) return $query;
if ( !empty($price_from) ) { // $price_from is set
$query->whereNotNull('sale_price')->where('sale_price', '>=', $price_from);
$query->orWhere('retail_price', '>=', $price_from);
} // if (!empty($price_from) ) { // $price_from is set
if ( !empty($price_to) ) { // $price_to is set
$query->whereNotNull('sale_price')->where('sale_price', '<=', $price_to);
$query->orWhere('retail_price', '<=', $price_to);
} // if (!empty($price_to) ) { // $price_to is set
return $query;
}
but setting 2 values 31 and 33 I have different result I expected :
https://imgur.com/a/AYTJAJn
I expected only the first row would be in results set!
In sql-editor I set "()" manually, as :
SELECT sale_price, retail_price
FROM articles
LEFT JOIN brands AS b on b.id = articles.brand_id
LEFT JOIN article_inventories AS ai on ai.article_id = articles.id
WHERE ( ( sale_price is not null AND sale_price >= '31' OR retail_price >= '31' ) AND
( sale_price is not null AND sale_price <= '33' OR retail_price <= '33') )
and it works as I need. If there is a way to set "()" in my scope?

Use to following constraint:
$price = 10;
$builder->where(function($query) use ($price) {
$query->whereNotNull('sale_price')->where('sale_price', '>', $price);
$query->orWhere('retail_price', '>', $price);
});
This will add the following to your query:
AND (sale_price IS NOT NULL and sale_price > 10 OR retail_price > 10)
What's important, you'll find those extra constraints wrapped in parenthesis which will let you avoid the situation, when other constraints are ignored because of the OR.

Related

How to get votes with results with percent calculating

In my Laravel 5.7/mysql 5 app I have a table with votes results:
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`vote_item_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`is_correct` TINYINT(1) NOT NULL DEFAULT '0',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
where boolean is_correct field is if answer was correct or incorrect.
I need to get data on percents of correct answers.
Creating such request
$voteItemUsersResultsCorrect = VoteItemUsersResult:: // Grouped by vote name
getByIsCorrect(true)->
getByCreatedAt($filter_voted_at_from, ' > ')->
getByCreatedAt($filter_voted_at_till, ' <= ')->
getByUserId($filterSelectedUsers)->
getByVote($filterSelectedVotes)->
getByVoteCategories($filterSelectedVoteCategories)->
getByVoteIsQuiz(true)->
getByVoteStatus('A')->
select( \DB::raw('count(vote_item_users_result.id) as count, votes.id, votes.name as vote_name') )->
orderBy('vote_name', 'asc')->
groupBy( 'votes.id' )->
groupBy( 'vote_name' )->
join(\DB::raw('vote_items'), \DB::raw('vote_items.id'), '=', \DB::raw('vote_item_users_result.vote_item_id'))->
join(\DB::raw('votes '), \DB::raw('votes.id'), '=', \DB::raw('vote_items.vote_id'))->
get();
I can get number of correct votes with sql request.
SELECT count(vote_item_users_result.id) AS count, votes.id, votes.name AS vote_name
FROM `vote_item_users_result`
INNER JOIN vote_items on vote_items.id = vote_item_users_result.vote_item_id
INNER JOIN votes on votes.id = vote_items.vote_id
WHERE `vote_item_users_result`.`is_correct` = '1' AND vote_item_users_result.created_at > '2018-08-01' AND vote_item_users_result.created_at <= '2018-09-22 23:59:59' AND `votes`.`is_quiz` = '1' AND `votes`.`status` = 'A'
GROUP BY `votes`.`id`, `vote_name`
ORDER BY `vote_name` asc
I know a way to get 2nd similar request with is_correct = '0' and on php side to combine results with percent calculating,
but I wonder if that could be done with eloquent in 1 request?
If yes, how ?
Thanks!
One correct raw MySQL would use conditional aggregation:
SELECT
v.id,
100.0 * COUNT(CASE WHEN vir.is_correct = 1 THEN 1 END) / COUNT(*) AS pct_correct,
100.0 * COUNT(CASE WHEN vir.is_correct = 0 THEN 1 END) / COUNT(*) AS pct_incorrect
FROM votes v
INNER JOIN vote_items vi
ON v.id = vi.vote_id
INNER JOIN vote_item_users_result vir
ON vi.id = vir.vote_item_id
WHERE
vir.created_at > '2018-08-01' AND vir.created_at < '2018-09-23' AND
v.is_quiz = '1' AND
v.status = 'A'
GROUP BY
v.id;
Now we can try writing Laravel code for this:
DB::table('vote')
->select('vote.id',
DB::raw('100.0 * COUNT(CASE WHEN vir.is_correct = 1 THEN 1 END) / COUNT(*) AS pct_correct'),
DB::raw('100.0 * COUNT(CASE WHEN vir.is_correct = 0 THEN 1 END) / COUNT(*) AS pct_incorrect'))
->join('vote_items', 'votes.id', '=', 'vote_items.vote_id')
->join('vote_item_users_result', 'vote_items.id', '=', 'vote_item_users_result.vote_item_id ')
->where([
['vote_item_users_result.created_at', '>', '2018-08-01'],
['vote_item_users_result.created_at', '<', '2018-09-23'],
['vote.is_quiz', '=', '1'],
['vote.status', '=', 'A']
])
->groupBy('vote.id')
->get();

Long Boolean Where in SQL

CLOSED. I had a mistake somewhere else unrelated to the boolean.
What am I doing wrong here in this SQL where:
WHERE (country = 'HK') OR (country = 'TW') OR (country = 'AX')
It’s missing the result for AX in the output.
What am I doing wrong with this Boolean expression?
Note1. ' at end of AX was my typo here. I corrected above.
Full code here
$myresult = mysqli_query($GLOBALS['DBlink'],
"SELECT $getcolumn
FROM levermann
WHERE
( $sqlwhere2 ) AND
levermann.`WEEK` =
(SELECT `WEEK`, COUNT(*) AS cc
FROM levermann
WHERE ( $sqlwhere2 )
GROUP BY `WEEK`
HAVING cc > 4
ORDER BY `WEEK` DESC
LIMIT 1 )
AND $lswitch AND $marketcap
ORDER BY LScore2 DESC, MarketCAPUSD DESC, Stock_Short ASC
LIMIT 10;");
if ($region == 'ASIA') {
$sqlwhere2 = "
( country = 'HK' ) OR
( country = 'TW' ) OR
( country = 'AX' ) OR
( country = 'KS' ) OR
( country = 'SS' )";
$region='Asia';
}
if ($region == 'Global') {
$sqlwhere2 = " country like '%'";
$region='Global';
}
if ($region == 'US') {
$sqlwhere2 = " country = 'US'";
$region='US';
}
There is a special character at the end enclosing 'AX‘
Also why not just use in() instead?
WHERE country in ('HK','TW','AX')
Entire statement should be in brackets
Edit: also wrong quote at the end
WHERE (country = 'HK' OR country = 'TW' OR country = 'AX')
you have error in the condition
(country = 'AX‘)
you are not using the ending '' correctly.please correct and check it

How to order not in stock items last

I have the following MySQL query:
SELECT
*
FROM
product
INNER JOIN productcontent ON product.id = productcontent.productID
WHERE
product.deactivate <> '1'
AND productcontent.price > 0 { $level1SQL }
AND menulevel IN ($imploded)
OR menulevel = '$menulevelID' { $level1SQL }
ORDER BY
(
CASE
WHEN discountPrice < price
AND discountPrice > 0 THEN
CAST(
discountPrice AS DECIMAL (10, 2)
)
ELSE
CAST(
price AS DECIMAL (10, 2)
)
END
) { $price_order }
LIMIT { $limit }
but I also need to make sure that if the product.notInStock set to 1, it is being ordered at the very end (but still by name / price if such sorting is selected), is it even possible or should I just use 2 queries - one for products that are in stock and one for those that aren't ?
You can add an additional key to the ORDER BY:
SELECT *
FROM product p INNER JOIN
productcontent pc
ON p.id = pc.productID
WHERE p.deactivate <> '1' AND
pc.price > 0 {$level1SQL} AND
(menulevel IN ($imploded) OR menulevel = '$menulevelID')
{$level1SQL}
ORDER BY p.not_inStock DESC,
(case when discountPrice < price and discountPrice > 0
then CAST(discountPrice AS DECIMAL(10,2))
else CAST(price AS DECIMAL(10,2))
end) {$price_order}
LIMIT {$limit}
Also, the last condition on menulevel suggests that you need parentheses.

How to write condition for subquery alias if having null value

Here is my query,
SELECT
`h`.`hotel_id`,
(
SELECT COUNT(room_id)
FROM
`abserve_hotel_rooms` AS `rm`
WHERE
`rm`.`adults_count` >= "1" AND `rm`.`room_count` >= "1" AND "Available" = IF(
check_in_time = '2016-03-15',
'Unavailable',
(
IF(
'2016-03-15' > check_in_time,
(
IF(
'2016-03-15' < check_out_time,
'Unavailable',
'Available'
)
),
(
IF(
'2016-03-22' > check_in_time,
'Unavailable',
'Available'
)
)
)
)
) AND `room_prize` BETWEEN '174' AND '600' AND `rm`.`hotel_id` = `h`.`hotel_id`
) AS `avail_room_count`,
(
SELECT MIN(room_prize)
FROM
`abserve_hotel_rooms` AS `rm`
WHERE
`rm`.`adults_count` >= "1" AND `rm`.`room_count` >= "1" AND "Available" = IF(
check_in_time = '2016-03-15',
'Unavailable',
(
IF(
'2016-03-15' > check_in_time,
(
IF(
'2016-03-15' < check_out_time,
'Unavailable',
'Available'
)
),
(
IF(
'2016-03-22' > check_in_time,
'Unavailable',
'Available'
)
)
)
)
) AND `room_prize` BETWEEN '174' AND '600' AND `rm`.`hotel_id` = `h`.`hotel_id`
) AS `min_val`
FROM
`abserve_hotels` AS `h`
WHERE
1 AND `city` = "madurai" AND `country` = "india"
It totally return one column values from my table abserve_hotels which is hotel_id with extra two alias columns such as avail_room_count and min_val..
And I wrote those in a subquery..
Here I have to check a condition WHERE min_val IS NOT NULL .i.e; if min_val having NULL value I have to restrict it
How can I do this..
And this is my table
hotel_id avail_room_count min_val
1 0 NULL
2 0 NULL
Here I need to restrict these NULL values..
Someone please help me ..
Add a HAVING clause at the end:
HAVING min_val IS NOT NULL
The new query after WHERE looks like:
WHERE
1 AND `city` = "madurai" AND `country` = "india"
HAVING min_val IS NOT NULL
Your query is overly complex and can be much simplified:
The two correlated sub queries are exactly the same, except for the SELECT list (MIN versus COUNT), so they could be combined into one;
The aggregation done by the sub query can be done in the main query;
The condition for checking availability can be written much shorter.
In fact, you can do all of what you need with the following query:
SELECT h.hotel_id,
COUNT(rm.room_id) as avail_room_count,
MIN(rm.room_prize) AS min_val
FROM abserve_hotels AS h
INNER JOIN abserve_hotel_rooms AS rm
ON rm.hotel_id = h.hotel_id
WHERE h.city = "madurai"
AND h.country = "india"
AND rm.adults_count >= 1
AND rm.room_count >= 1
AND rm.room_prize BETWEEN 174 AND 600
AND ( rm.check_in_time >= '2016-03-22'
OR rm.check_out_time <= '2016-03-15'
OR rm.check_in_time IS NULL)
GROUP BY h.hotel_id
Because the INNER JOIN requires at least one match, you can already be sure that min_val will never be NULL.
The check for availability is just as simple as:
( rm.check_in_time >= '2016-03-22'
OR rm.check_out_time <= '2016-03-15'
OR rm.check_in_time IS NULL)
The three parts of that condition mean:
The reservation for this room is future and does not overlap with this week;
The reservation for this room is in the past, the room is free today at the latest;
The room has no known reservation.
In all three cases the room is available for reservation for the concerned week.

Mysql return null if subquery returns null

Hy guys, sometimes my subquery return null which is ok, it should return null, but in those cases i would like my "parent select" to return null.
Is that possible?
And if yes, then how?
Heres the code:
SELECT
`company`.`companyID`,
`company`.`companyName`,
`company`.`companyName`,
`company`.`companyEmail`,
`company`.`contactEmail`,
`company`.`companyTel`,
(
SELECT
`package_map`.`szekhely_endDate`
FROM
`package_map`
WHERE
`package_map`.`companyID` = `company`.`companyID`
AND
`package_map`.`active` = 1
AND
`package_map`.`szekhely_endDate` > NOW()
ORDER BY
`package_map`.`szekhely_endDate` DESC
LIMIT 1
) as endDate,
CASE
WHEN endDate = NULL
FROM
`company`
WHERE
`company`.`companyBase` = 'some address'
AND
`company`.`szekhely_check_out` = 0
Use an ordinary INNER JOIN between the two tables. If there's no matching rows in the package_map table, there won't be a row in the result. To get the latest endDate, use the MAX() function.
SELECT
`company`.`companyID`,
`company`.`companyName`,
`company`.`companyName`,
`company`.`companyEmail`,
`company`.`contactEmail`,
`company`.`companyTel`,
MAX(package_map.szekhely_endDate) AS endDate
FROM company
INNER JOIN package_map ON `package_map`.`companyID` = `company`.`companyID`
WHERE
`company`.`companyBase` = 'some address'
AND
`company`.`szekhely_check_out` = 0
AND
`package_map`.`active` = 1
AND
`package_map`.`szekhely_endDate` > NOW()
GROUP BY `company`.`companyID`