php mysql compare values - php

I have an mysql database named gpstracking.
There are a couple tables in this database: positions and devices.
What is the issue.
On the table positions we have a row called device_id.
On the table devices we have a row called id.
On the table devices we have a row called name.
The device_id and id rows are the same id's, they are pointed to get the name.
My problem is, how to in PHP a loop or something to get the name when compare the 2 id rows.
So example:
device_id row:
1
2
3
4
5
id row:
1 Jan
2 Klaas
3 Piet
So if i fetch the information from table with the device_id row, i want that the script can get the name from the device_id.
Can someone help me ?

You mean this:
Select * from positions
join devices ON positions.device_id = devices.id

Related

How to retrieve data from table which is separated by comma in sql?

I have table students containing data like given below :
id name activity
1 susan 1,2
2 denny 1,2,3
3 ken 3
I want to choose all the data like name and id from this table having activity equal to 2. I have done a sql query using like but it will not work.
$sqll = mysqli_query($con,"SELECT id,studentname FROM students WHERE activity LIKE '%$activity'");
If another row contains data like id : 4 name:ben activity : 22 it will show this row also. I want the rows containing activity 2 only.
Can anyone suggest a solution for this ?
I agree with the comments so far about normalising the data but to answer your question with the table structure as it is you can do this:
$sqll = mysqli_query($con,"SELECT id,studentname FROM students WHERE CONCAT(',',activity,',') LIKE '%,$activity,%'");
Note, you concatenate a comma either side of the field data and the specific activity you are looking for. Also note, I added a % on the right of the like as well so that "denny" will be caught
SELECT id, studentname
FROM students
WHERE FIND_IN_SET("$activity", activity);

How to use find_in_set with join in codeigniter

I have 4 tables, and I want to fetch data from all the tables, I can do this by fetching data one by one from each table but I want to do it by using JOIN.
Main Table (which contains ids of other table data)
2, 3, 4 tables
now I want to fetch these fields.
from Table 1 (Main Table) - franchise_name, franchise_phone
from Table 2 (State Table) - state_name
from Table 3 (City Table) - city_name
from Table 4 (Area Table) - area_name
the first table contains ids of everything which I need to fetch from other tables.
but area_id in the main table is inserted as a string in the same row separated by (,) in field franchise_area.
I tried using FIND_IN_SET but did not work.
Read all 1-1 referred data using Join, cycle through data exploding area_id column
area_id -------> ($value = explode($row->area_id, ',')
then read data from database and insert into response array (or object).
Of course all of this operation must be done into the model...

Cakephp Find removed rows of two joined tables

I have table A and table B and they are both joined through a relationship. table B has a foreign key of table A's key. Table A is joined when the program runs because it is dynamic and could reside in any database. How would I find all the rows in table B that are no longer found in table A?
For ex.
Starting out we have
table A table B
4 4
3 3
5 5
later that evening
table A deleted some rows
table A table B
NULL 4
3 3
NULL 5
I want to get the keys 4,5 so that I can tell what was deleted.
Just use mysql where not in table. I would just use cakephp query method versus find. Here is a topic that already has been answered: Where not in table

Compare 2 columns from different tables and return different values of 2nd table

I have two tables. Second table has a column that stores the primary id of the First table. I want to return different rows from the first table that are not in the second table.
example Table 1
id/
12. value 1
21. value 2
34. value 3
41. value 4
second table
id/
1. value 12
2. value 6
3. value 41
I want to return the opposite rows in table one where the values match from second table; e.g return value 2 and 3 in this example from table 1 where id is not 12 or 41.
Sounds like something you should do directly in the query. Your question is a bit unclear, bit if you want to get all rows from table 1 whose ids are not stored in table 2, you can do this:
SELECT * FROM table1 WHERE id NOT IN (SELECT id FROM table2)
SELECT * FROM TABLE1 WHERE ID NOT IN (SELECT ID FROM TABLE2)

Query to select latest record when using Group By - MySQL

There is a table structure:
ID1 (int), ID2 (int), AddedAt (datetime)
where ID1 and ID2 are primary keys. Now This table contains already up to 300,000 records and I need to restructure the table to have 1-1 relationship. Meaning i need to drop the ID1 from being primary key and make only ID2 to be the primary key but there are records that already contain multiple ID2s for each ID1.
Now before i do that I need to have a query to select all the records where multiple ID2 exists and remove them, only need to leave one record ID2 and that record has to be latest one which is determined by AddedAt datetime column.
This is what i have so far:
SELECT *, COUNT(*) AS Total
FROM TestTable
GROUP BY ID2
HAVING Total > 1
this successfully returns all records but AddedBy does not come out to be the latest one. What do i need to add to this query to get the latest record only 1 for Grouped By ID2 so that i can make the ID2 to be the primary key.
If this cant be done in MySQL can it be done in PHP?
this successfully returns all records but AddedBy does not come out to be the latest one.
Most SQL dialects forbid selecting column values that are not part of the GROUPing – MySQL allows it (depending on server settings), but delivers a value from a random row for these columns.
What you want are the Rows Holding the Group-wise Maximum of a Certain Column.

Categories