UNION SELECT 和初始化用户定义的变量

问题描述

我有一个相对较长的联合选择:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.UI;

public class ContinuousMovement : MonoBehavIoUr
{
    //This script is used to make the VR Camera moves continuous using the analog
    public float speed = 1;
    public XRNode inputSource;
    public float gravity = -9.81f;
    public LayerMask groundLayer;
    public float additionalHeight = 0.2f;
    public List<GameObject> UiToBeHidden;

    private float fallingSpeed;
    private XRRig rig;
    private Vector2 inputAxis;
    private CharacterController character;

    // Start is called before the first frame update
    void Start()
    {
        character = GetComponent<CharacterController>();
        rig = GetComponent<XRRig>();

        foreach (GameObject ui in UiToBeHidden)
        {
            ui.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
        CapsuleFollowHeadset();
        InputDevice device = InputDevices.GetDeviceAtXRNode(inputSource);
        device.TryGetFeatureValue(CommonUsages.primary2DAxis,out inputAxis);

        Quaternion headYaw = Quaternion.Euler(0,rig.cameraGameObject.transform.eulerAngles.y,0);
        Vector3 direction = headYaw * new Vector3(inputAxis.x,inputAxis.y);

        character.Move(direction * Time.fixedDeltaTime * speed);

        if (inputAxis.x == 0 && inputAxis.y == 0)
        {

            Debug.Log("static");
            foreach (GameObject ui in UiToBeHidden)
            {
                ui.SetActive(true);
            }
        }
        else
        {
            Debug.Log("moving");
            foreach (GameObject ui in UiToBeHidden)
            {
                ui.SetActive(false);
            }
        }
    }

 

    private void FixedUpdate()
    {
        //gravity
        bool isGrounded = CheckIfGrounded();
        if (isGrounded)
            fallingSpeed = 0;
        else
            fallingSpeed += gravity * Time.fixedDeltaTime;

        character.Move(Vector3.up * fallingSpeed * Time.fixedDeltaTime);
    }

    void CapsuleFollowHeadset()
    {
        character.height = rig.cameraInRigSpaceHeight + additionalHeight;
        Vector3 capsuleCenter = transform.InverseTransformPoint(rig.cameraGameObject.transform.position);
        character.center = new Vector3(capsuleCenter.x,character.height /2 + character.skinWidth,capsuleCenter.z);
    }

    bool CheckIfGrounded()
    {
        //tells us if on ground
        Vector3 rayStart = transform.TransformPoint(character.center);
        float rayLength = character.center.y + 0.01f;
        bool hasHit = Physics.SphereCast(rayStart,character.radius,Vector3.down,out RaycastHit hitInfo,rayLength,groundLayer);
        return hasHit;
    }
}

我想将 xxx 定义为用户定义的变量,这样我就不必多次重复该条件。不幸的是,我的 MysqL 客户端(可视化)不支持多条语句,所以一开始我不能使用 SET。有没有办法在不改变 sql 输出的情况下将用户定义的变量包含到这样的语句中?

解决方法

您几乎可以在查询的任何位置使用 @varname:='value'。在这种情况下,您可以:

select A,B 
from table1 
JOIN (SELECT @x:='xxx') x 
where C=@x
union select A,B from table2 where C=@x
union select A,B from table3 where C=@x
...

或者您可以再添加一个联合:

SELECT null,null FROM (SELECT @x:='xxx') x WHERE 0
union select A,B from table1 where C=@x
union select A,B from table3 where C=@x
...

你可以把@x:='xxx'放在任何方便的地方,只要注意执行顺序即可。