changing during accomplish query - php

I've such a interested question for me , please if anyone know answer me .
So I have two table , in first i have column named "num" which includes (like say phone numbers) and in second table i have column "sec_num" which also includes some of the exactly same numbers like in first table from column "num" and second column named "persons" (from second table also)
I am retrieving data from first table and i want that if first table phone numbers (from num column) matched to second table numbers (from column "sec_num") then retrieved value from "persons" column. Is it possible to do ? if yes , please help me , Thanks ...
PS. if it is necessary to know i am using php, but I think this job must do sql

If I understand the question right a regular left join should do the trick;
select num, persons from first left join second on num=sec_num;

If we call the tables tableA (first table) and tableB (second table) thern a query like:
SELECT * FROM tableA INNER JOIN tableB on tableA.num = tableB.sec_num
should do the trick.

Related

Need help in a sql query

I have a field that is a varchar that contain values such as (88,90,100,200) and i have another one contains the value 200 when using the IN clause to see if the second field in the first field it returns empty result but when comparing it with 88 it return results
So i was wondering what im doing wrong here and if there is a better way.
Here is the mysql code
select * from user inner join category where parent_id IN (categories)
parent_id is located in the category table and the categories in the user table
When you do a JOIN you need an ON clause to specify that you are looking for rows that somehow "match" across two tables. If you want all the rows from a single table where some column has a value that is "one of the values in this list..." you use IN. So these are both valid SQL:
SELECT * FROM user INNER JOIN category ON user.someColumn = category.someOtherColumn // can add WHERE clause
or
SELECT * FROM user WHERE parent_id IN (88, 99, 100, 200)
Can't tell you exactly what query you should use unless you share your table structures, as the question is unclear.

Procedure that for looks for an id in different table and inserts it if there is no entry for that id

I need to write a procedure in mysql that runs every night and performs the following:-
For each 'id' from 'associate' table find if there is an entry in 'status table' for a particular 'department' . If there is an entry then move on to next 'id' else insert a row in status table for that id.
Associate Table
Assoc_Id
Assoc_Project
Assoc_Assigned_On
Status Table
Sub_id
Assoc_Id
Sub_on
Department
I just have no clue about how to do this. Any help will be highly appreciated.
Thanks in advance
Since you haven't provided a full schema, I'm unsure of primary/foreign keys on the tables, but here is a query that can help you:
INSERT INTO tbl_status (Assoc_Id, Sub_on, Department)
SELECT a.Assoc_Id, [Your Sub_on Value], 'IT'
FROM tbl_associate a
LEFT JOIN tbl_status s ON a.Assoc_Id = s.Assoc_Id AND s.Department = 'IT'
WHERE s.Sub_Id IS NULL;
The basic idea here is to LEFT JOIN on the status table from the associate table where the Assoc_Id's are equal and the department value is the one you're searching for. The WHERE clause filters the results so that it only shows records that are not currently in the status table based on the join condition. I chose Sub_Id because I assumed that is the primary key. It doesn't really matter what you choose here except that the value has to be a non-null field in the status table order for this query to work.

How to use colum name as a value and filter highest value MYSQL/PHP

This is my SqlFiddle I have managed to get all values i need except i havent figured out how to check which value from table "tickets" has highest value as column name in table "draws" and then assign value from table "coefficients".
So, if table tickets have values: 9,12,1,11,14,10
And table "draws" have values: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,21,23,24,25,26,27,28,29,30,31,32,33,1,35
Highest value column name from this "ticket" in table draws is 4th value in table tickets (number 1) and because that is 34th value in table draws i should get 34th value from table "coefficients"
Some PHP or even vb.net function would be ok because i need this for a simple application and it does not matter how it's done as long as it is correct.
Oh, yes... i forgot to put my sql here:
SELECT *,
c.coefficient
FROM tickets t
INNER JOIN draws d ON
(FIELD(t.b1,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b2,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b3,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b4,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b5,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
INNER JOIN coefficients c
ON c.number = FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)
WHERE draw='1' AND status = '1'
Any help would be awesome.
I think you can use GREATEST() function
try this sqlFiddle

Selecting columns without specifying column name

I have a very strange scenario and would be highly grateful if you can help me out. I have one flat table of five fields, and I need to select everything from say the last three columns.
SAMPLE:
ID CODE BRAND T1 T2
== ==== ===== == ==
1 KUU RR4 1 2
2 KUU R56 2 1
=====================
I need only the values of T1 and T2. The reason I need to get the last two columns is that the table is generated automatically from an Excel file, so the first three columns are static (ID, CODE, BRAND), and only last two columns are dynamic (T1, T2) and can be different next time, like (P1, P2), so I cannot do something like SELECT T1, T2 FROM sampleTable. I am totally stumped any help or advice would be great.
You didn't mention php, but there it is in the tags so:
In your php script, you could select the whole shebang and still access the fields in a numeric order. For example:
$q=mysqli_query("SELECT * FROM SAMPLE");
while($row=mysqli_fetch_array($q,MYSQLI_NUM))
{
printf("col#4: %s -- col#5: %s",$row[3],$row[4]);
}
SELECT 4,5 FROM SampleTable;
4,5 specifies the column number in your table.

How to set the relative order of columns with a zfdatagrid?

To those of you who are familiar with zfdatagrid for the Zend framework:
I got a recordset by a JOIN query, say from tables s and t, now I want to set the order in which the columns appear in the deployed table.
For example,
5th column of table t to appear at the leftmost side,
then 3rd column of table s,
then 2nd column of table t,
then 4th column of table s.
How do I do this? Whatever I tried, it always shows first the columns of the left table of the JOIN, then the columns of the right table of the JOIN.
I only know how to tell it which columns to show, but not their order.
It may be late but may be useful for others
You can use:
$grid->updateColumn($field,array('position'=>$i))

Categories