Laravel select where/count query - mysql

There are two Mysql tables:
1.reservations(id,screening_id,reserved). the reserved is a boolean its value "1".
table seat_reserveds(id,screening_id,reservation_id,seat_id).
I want the following:
1.get the reservation id
select id from reservation where screening_id = id and reserved = 1
In laravel looks like:
$reservation_id = DB::table('reservations')->select('id')-
>where('screening_id',$id)->where('reserved','1')->get();
Then I want to count how many seats are reserved
Select count(id) from seat_resrveds where where screening_id = id and reservtain_id = id.
In laravel looks like:
$reservedtickets = DB::table('seat_reseverds')-
>where('screening_id',$id)->where('reservation_id',$reservation_id)->count('id');
I think the problam is that I get "id":number from $reservation_id so cant use it at the $reservedtitckets query because I only need a number.
How can I write these querys.

Use ->value('id') to get a single value from a query:
$reservation_id = DB::table('reservations')
->where('screening_id',$id)
->where('reserved','1')
->value('id');

Update the query to the following.
$reservation = DB::table('reservations')
->where('screening_id',$id)
->where('reserved', 1)
->first();
Then the $reservation_id should be as below.
$reservation_id = $reservation->id;
You can pass this parameter to your next query for $reservedtickets.

Related

Count number of entries made by id in a table in Yii2?

I have a table like this now what i want to do is count the records for each ID where STATUS is COMPLETED
.
Something like SELECT COUNT(*) FROM TABLE WHERE ID= foreach(ID)
AND STATUS=1
select id, count(id) as count from table where status='COMPLETED' group by id;
Basically you need to use group by clause in MySQL.
You can do it using Active Record as this:
I assuming you have you table model named like Job, then you can get a needed value as this:
$number = Job::find()->where(['STATUS' => 'Completed'])->count();
or it is always better to store constant properties in the model like this:
class Job extends ActiveRecord {
const STATUS_COMPLETED = 'Completed';
then your ActiveQuery will look like this:
$number = Job::find()->where(['STATUS' => Job::STATUS_COMPLETED])->count();
Also here is the full description: Querying Data
$any_var = Your_Table::find()->where(['status' => 'Cimpleted'])->select('id')->orderBy('id')->groupBy('id')->count ();

How to count all records if use alias in select query?

I use Sphinx with Yii2 and need to query with filter by jSON field.
$query = new \yii\sphinx\Query();
$query->from('announcements');
$query->addSelect("*");
$query->addSelect(new Expression("IN(filters['color'], 'blue', 'red', 'green') AS f_color"));
$query->where("is_active = 1");
$query->andWhere("f_color = 1");
$announces = $query->all();
There is jSON field filters in my Sphinx index. For example:
[filters] => {"brand":"Toyota","model":"Prius","color":"red","price":"12000"... etc]
It works OK. But now I need to make a pagination... and there is a problem when I try to count records before $query->all()
$count = $query->count(); // Return error "no such filter attribute 'f_color'"
Generated query was:
SELECT COUNT(*) FROM announcements WHERE ( is_active = 1 ) AND ( f_color = 1 )
count() by default replaces the select part with * and this is where your alias is defined hence the error.
There are different ways to achieve it like:
use ActiveDataProvider like described here,
use META information like described here
Since you want to make a pagination I would go with the first example.

How do I put a previous result into the next query? MYSQL

Okay so, this is my query.
select id from rooms where owner = 'oknow';
and the answer I get is
325
However, I made another SQL within this one as below
update users set home_room = 'mysql_fetch_assoc()' where username = 'omarisgod';
I want the 'mysql_fetchassoc()' to be the '325' value, how do I do this?
A subquery will do this:
UPDATE users SET home_room = (SELECT id FROM rooms WHERE owner = 'oknow') WHERE username = 'omarisgod';
You can conceptualize it thusly: The query inside parentheses will return a result, which will be utilized by the outer query.

mysql how to select from one table if the second table has no matching information

I am trying to only show results from table inventoryfolders if the id does not exist in table newlisting.
Here is my query:
$sql = "SELECT *
FROM avirobust.inventoryfolders
WHERE parentFolderID = '".$foname."'
AND agentID = '".$mne."'
AND parentFolderID NOT IN (SELECT folderID FROM marketplace.newlisting)";
However it never returns a result even if newlisting has no data in it.
I know that join can work but can I use something like in my query above? If so what did I do wrong in my query?
You can try use this code
SELECT inventoryfolders.*
FROM avirobust.inventoryfolders LEFT JOIN marketplace.newlisting on (inventoryfolders.parentFolderID = newlisting.folderID)
WHERE parentFolderID = '".$foname."'
AND agentID = '".$mne."'
AND newlisting.folderID is NULL
It will return all records from inventoryfolders where no corresponding rows in newlisting

Facing issue with SQL query in the where clause

I have the following database scheme on MySQL and I would like to retrieve all elements for a speciic id.
So for instance, I would like to retrieve cities, categories, departments linked to the coupon_id=1 (and other fields).
I wrote the following SQL query but unfortunatelly could not get the desired result.
SELECT cc_coupon.id_coupon as idCoupon,
cc_coupon.condition_coupon,
cc_coupon.description,
cc_coupon.type_coupon,
cc_coupon_by_categorie.id_categorie,
cc_categorie.categorie as category,
cc_annonceur.raison_sociale,
cc_coupon_active_in_cities.id_ville as ville_slug,
cc_villes_france.ville_slug,
cc_villes_france.ville_nom_departement,
cc_villes_france.ville_departement
FROM cc_coupon,
cc_coupon_by_categorie,
cc_categorie,
cc_annonceur,
cc_coupon_active_in_cities,
cc_coupon_active_in_departments,
cc_villes_france
WHERE cc_coupon.id_coupon = cc_coupon_by_categorie.id_coupon
and cc_categorie.id_categorie = cc_coupon_by_categorie.id_categorie
and cc_coupon.id_annonceur = cc_annonceur.id_annonceur
and cc_coupon.id_coupon = cc_coupon_active_in_cities.id_coupon
and cc_villes_france.id_ville = cc_coupon_active_in_cities.id_ville
and cc_villes_france.ville_departement = cc_coupon_active_in_departments.ville_departement
and cc_coupon.id_coupon = 1
and cc_coupon_active_in_cities.id_coupon = 1
and cc_coupon_active_in_departments.id_coupon = 1
Thanks for your help.
I think you should use the on and not where when you want to join two tables. When you want to specify other conditions use where clause.