Return alphanumeric values as id in postgresql - php

I have table called tickets with p.k. id and ticket number (i.e. TK0001)
I am using a laravel and for some reason I am required to get the ticket_number as id in response.
Below is my query
SELECT CAST(ticket_number as varchar) as id from
tickets.
Below is my response
{
"status": 200,
"data": {
"current_page": 1,
"data": [
{
"id": 0
},
{
"id": 0
}
],
"first_page_url": "http://localhost:8000/dm/v2/report/preview/data?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost:8000/dm/v2/report/preview/data?page=1",
"next_page_url": null,
"path": "http://localhost:8000/dm/v2/report/preview/data",
"per_page": 100,
"prev_page_url": null,
"to": 2,
"total": 2
}
}
If you see it's returning me "id": 0
But it returns me the ticket number when using the alias name different like ids.
Here are the data from my table
id ticket_number
22021 "TK33412"
22022 "TK15961"
22023 "TK15840"
22024 "TK33413"
22025 "TK33416"
22026 "TK33415"
here is my query
$mainSelect = "CAST(ticket_number as varchar) as id ";
$query = \App\Models\DM\LeadsTickets::selectRaw($mainSelect);
$queryResponse = $query->paginate($perPage , $page);
return $queryResponse;

Related

MYSQL - Combine 2 tables in one query BUT have 2 WHERE/IN clauses

I am looking to do one MYSQL query on 2 tables and get the results as one object/array. My problem is that the query needs to matcha set of IDs that are named differently in both tables.
Let's say I have a urls table and a products table and they both have a column that corresponds to the ID I'm searching for but named differently... in the urls table its called resource_id, and in the products table its called productid.
URLS TABLE:
id | resource_id | url
PRODUCTS TABLE:
productid | title | description | thumbnail
So in php I have a list of IDs that use to retrieve both tables combined BUT only where the urls.resource_id and products.productid equal those IDs.
Here is what i have so far:
$ids = ["18552", "18554", "18555", "18556"];
$newIds = implode(",",$ids);
$q3 = " SELECT products.title as title, urls.url as url
FROM urls, products
WHERE urls.resource_id IN ($newIds)
AND products.productid IN ($newIds)";
At the moment this returns the 4 results based on the $ids variable 4 times - 16 results in total. I think it has to do with the WHERE urls.resource_id IN ($newIds) AND products.productid IN ($newIds portion of the query since 4 * 4 = 16, but I could be wrong.
Something like this:
{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },
{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },
{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },
{ "title": "Prod 1", "url": "prod-one.html" },
{ "title": "Prod 2", "url": "prod-two.html" },
{ "title": "Prod 3", "url": "prod-three.html" },
{ "title": "Prod 4", "url": "prod-four.html" },
Not really sure how to achieve this without the repeats... any suggestions?
ALSO - this database is from a shopping cart system so I tried to simplify the example as much as possible without losing clarity in my question/example.
Thank you in advance!
Please try this query. It shows all the entrys that exist in both tables and a in $tdis
$ids = ["18552", "18554", "18555", "18556"];
$newIds = implode(",",$ids);
$q3 = " SELECT p.title as title, u.url as url
FROM urls u INNER JOIN products p ON u. resource_id = p.productid
WHERE u.resource_id IN ($newIds);";

Add Data from JSON in database in two different tables

I have a JSON file which contains data that i want to store in two different database in sql.
The json file looks like this:
[
{
"title": "CONSERVE IT LTD",
"address": "12 Truman Ave (10) ",
"phones": [
{
"name": "telephone_1",
"number": "876-754-0220"
},
{
"name": "telephone_2",
"number": "876-754-0221"
}
],
"website": "www.conserveitja.com",
"page": 1
},
{
"title": "Consie Walters Cancer Hospital",
"address": "22 Deanery Rd (3) ",
"phones": [
{
"name": "telephone_1",
"number": "876-930-5016"
}
],
"page": 1
},
...
]
I managed to store title, address, website and page in my 1st table in the database but now I want to store in a different table in the same database the phones for each business.
Here is the code I am using to store the information for the first table and I little code for the 2nd table.
$connect = mysqli_connect("localhost", "root", "1234", "businesses"); //Connect PHP to MySQL Database
$query = '';
$query_phones='';
$table_data = '';
$filename = "businesses.json";
$businesses = file_get_contents($filename);
$business = json_decode($businesses, true);
foreach($business as $row)
{
$query .= "INSERT INTO business(title, address, website, page) VALUES ('".$row["title"]."', '".$row["address"]."', '".$row["website"]."', '".$row["page"]."'); ";
//data that i will show on page
$table_data .= '
<tr>
<td>'.$row["title"].'</td>
<td>'.$row["address"].'</td>
<td>'.$row["website"].'</td>
<td>'.$row["page"].'</td>
</tr>
';
}
Code for inserting the phone data in the second table in database
foreach($business as $row)
{
$query_phones .="INSERT INTO business_phones(business_title, phone_number, phone_name) VALUES ('".$row["title"]."', '".$row["number"]."', '".$row["name"]."');";
}
Structure of 1st table (business)
1 idPrimary int(11)
2 title varchar(50)
3 address varchar(50)
4 website varchar(50)
5 page int(11)
Structure of 2nd table (business_phones)
1 idPrimary int(11)
2 business_title varchar(250)
3 phone_number varchar(255)
4 phone_name varchar(255)

Select inside a select query MYSQL

Hello! I have a query that needs to output screens data.
Each screens has 4 booths to be shown. I need a query with one result (because I just have one screen) And has 4 booths in it (coz I assigned 4 booths)
Here's my query:
$result = DB::select("
SELECT
`A`.`scr_name`,
`A`.`mission_id`,
`B`.`booth_id`,
`E`.`name`
FROM `tbl_screen` `A`
LEFT JOIN
`tbl_screen_booths` `B`
ON `B`.`screen_id` = `A`.`id`
LEFT JOIN
`tbl_service_booths` `C`
ON `B`.`booth_id` = `C`.`id`
LEFT JOIN
`tbl_missions_services` `D`
ON `C`.`mission_services_id` = `D`.`id`
LEFT JOIN
`tbl_services` `E`
ON `D`.`service_id` = `E`.`id`
WHERE `A`.`mission_id` = $mission_id
GROUP BY
`B`.`booth_id`;
");
return $result;
I want something like this:
"scr_name": "Test Screen",
"mission_id": 2,
"name": "booth1", //index[0]
"name": "booth2", //index[1]
"name": "booth3", //index[2]
"name": "booth4" //index[4]
My query returns something like this:
[
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 7,
"name": "booth1"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 9,
"name": "booth2"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 10,
"name": "booth3"
},
{
"scr_name": "Test Screen",
"mission_id": 2,
"booth_id": 11,
"name": "booth4"
}
]
the result is okay.. just need a little iterate to get the value..
$data['scr_name'] = $result[0]->scr_name;
$data['mission_id'] = $result[0]-> mission_id;
foreach($result as $index => $item) {
$data['name'.($index+1)] => $item->name;
}
result:
"scr_name": "Test Screen",
"mission_id": 2,
"name1": "booth1", //index[0]
"name2": "booth2", //index[1]
"name3": "booth3", //index[2]
"name4": "booth4" //index[4]
array key cant have same key name

Merge 2 different tables without repeating values PHP

I have 2 tables; the first one is user_in_event table
which here role can be regular, waiting for ack or moderator
and the second one is user table
Here the role can be regular or admin
I want to take the role from user_in_event table, based on the event_id and output all users info (beside user role) and output in a single JSON
I have tried to use LEFT JOIN
$query = "SELECT user_in_event.role, user_in_event.user_id, user.name, user.email, user.birthday, user.phone_number, user.address, user.image
FROM user LEFT JOIN user_in_event
ON user_in_event.event_id = '".$event_id."'
LIMIT $num_of_rows";
$result = mysqli_query($con,$query)
or die(mysqli_error($con));
// print_r($query);
while ($row = mysqli_fetch_assoc($result)) {
$response[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($response);
but I got messed up data
[
{
"role": "moderator",
"user_id": "2",
"name": "ofir",
"email": "ofir#ofr.com",
"birthday": "08/12/2016",
"phone_number": "123",
"address": "yoqneam",
"image": "http://imgur.com/a/KslOW"
},
{
"role": "waiting for ack",
"user_id": "21",
"name": "ofir",
"email": "ofir#ofr.com",
"birthday": "08/12/2016",
"phone_number": "123",
"address": "yoqneam",
"image": "http://imgur.com/a/KslOW"
}
]
PS: I also tried to make 2 different queries and combine the JSON and user array_merge_recursive but I got 2 subs arrays instead of a single one
Please try :
SELECT user_in_event.role, user_in_event.user_id, user.name, user.email
FROM user
LEFT JOIN user_in_event ON user_in_event.user_id = user.user_id
WHERE user_in_event.event_id =25
LIMIT 0 , 30
Here event_id is static. run this query to your database and let me know.

Get JSON from OneToOne relation using propel

I'm going to get a JSON like following structure from my database:
{
"Users": [
{
"Id": 1,
"Name": "a",
"Family": "b",
"RegisterId": 1,
"AccessType": 1,
"Username": "abc"
},
{
"Id": 2,
"Name": "x",
"Family": "y",
"RegisterId": 2,
"AccessType": 1,
"Username": "xyz"
}
]
}
Id,Name,Family,RegisterId,AccessType are User_TBL columns and Username is Register_TBL column.
I can get this JSON using below query:
SELECT u.id,u.name,u.family,u.access_type,u.register_id,r.username
FROM `user` as u LEFT JOIN `register` as r
ON u.register_id = r.id
I'm trying to use below line using propel, but it just shows all User_TBL columns.
$userList = UserQuery::create()-> joinWith('User.Register')-> find();
What's your suggestion ?!
I used below code and it solved my problem.
$userList = UserQuery::create()
-> leftJoinRegister()
-> select(array('ID','NAME','FAMILY','AccessType'))
-> withColumn('Register.Username' , 'Username')
->find();

Categories